Vladimir Golovnev
2 years ago
committed by
GitHub
11 changed files with 615 additions and 4 deletions
@ -0,0 +1,195 @@
@@ -0,0 +1,195 @@
|
||||
/*
|
||||
* Bittorrent Client using Qt and libtorrent. |
||||
* Copyright (C) 2023 Vladimir Golovnev <glassez@yandex.ru> |
||||
* Copyright (C) 2016 The Qt Company Ltd. |
||||
* |
||||
* This program is free software; you can redistribute it and/or |
||||
* modify it under the terms of the GNU General Public License |
||||
* as published by the Free Software Foundation; either version 2 |
||||
* of the License, or (at your option) any later version. |
||||
* |
||||
* This program is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License |
||||
* along with this program; if not, write to the Free Software |
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||||
* |
||||
* In addition, as a special exception, the copyright holders give permission to |
||||
* link this program with the OpenSSL project's "OpenSSL" library (or with |
||||
* modified versions of it that use the same license as the "OpenSSL" library), |
||||
* and distribute the linked executables. You must obey the GNU General Public |
||||
* License in all respects for all of the code used other than "OpenSSL". If you |
||||
* modify file(s), you may extend this exception to your version of the file(s), |
||||
* but you are not obligated to do so. If you do not wish to do so, delete this |
||||
* exception statement from your version. |
||||
*/ |
||||
|
||||
#include "flowlayout.h" |
||||
|
||||
#include <QWidget> |
||||
|
||||
#include "base/global.h" |
||||
|
||||
FlowLayout::FlowLayout(QWidget *parent, const int margin, const int hSpacing, const int vSpacing) |
||||
: QLayout(parent) |
||||
, m_hSpace {hSpacing} |
||||
, m_vSpace {vSpacing} |
||||
{ |
||||
setContentsMargins(margin, margin, margin, margin); |
||||
} |
||||
|
||||
FlowLayout::FlowLayout(const int margin, const int hSpacing, const int vSpacing) |
||||
: m_hSpace {hSpacing} |
||||
, m_vSpace {vSpacing} |
||||
{ |
||||
setContentsMargins(margin, margin, margin, margin); |
||||
} |
||||
|
||||
FlowLayout::~FlowLayout() |
||||
{ |
||||
QLayoutItem *item; |
||||
while ((item = takeAt(0))) |
||||
delete item; |
||||
} |
||||
|
||||
void FlowLayout::addItem(QLayoutItem *item) |
||||
{ |
||||
m_itemList.append(item); |
||||
} |
||||
|
||||
int FlowLayout::horizontalSpacing() const |
||||
{ |
||||
if (m_hSpace >= 0) |
||||
return m_hSpace; |
||||
|
||||
return smartSpacing(QStyle::PM_LayoutHorizontalSpacing); |
||||
} |
||||
|
||||
int FlowLayout::verticalSpacing() const |
||||
{ |
||||
if (m_vSpace >= 0) |
||||
return m_vSpace; |
||||
|
||||
return smartSpacing(QStyle::PM_LayoutVerticalSpacing); |
||||
} |
||||
|
||||
int FlowLayout::count() const |
||||
{ |
||||
return m_itemList.size(); |
||||
} |
||||
|
||||
QLayoutItem *FlowLayout::itemAt(const int index) const |
||||
{ |
||||
return m_itemList.value(index); |
||||
} |
||||
|
||||
QLayoutItem *FlowLayout::takeAt(const int index) |
||||
{ |
||||
if ((index >= 0) && (index < m_itemList.size())) |
||||
return m_itemList.takeAt(index); |
||||
|
||||
return nullptr; |
||||
} |
||||
|
||||
Qt::Orientations FlowLayout::expandingDirections() const |
||||
{ |
||||
return {}; |
||||
} |
||||
|
||||
bool FlowLayout::hasHeightForWidth() const |
||||
{ |
||||
return true; |
||||
} |
||||
|
||||
int FlowLayout::heightForWidth(const int width) const |
||||
{ |
||||
const int height = doLayout(QRect(0, 0, width, 0), true); |
||||
return height; |
||||
} |
||||
|
||||
void FlowLayout::setGeometry(const QRect &rect) |
||||
{ |
||||
QLayout::setGeometry(rect); |
||||
doLayout(rect, false); |
||||
} |
||||
|
||||
QSize FlowLayout::sizeHint() const |
||||
{ |
||||
return minimumSize(); |
||||
} |
||||
|
||||
QSize FlowLayout::minimumSize() const |
||||
{ |
||||
QSize size; |
||||
for (const QLayoutItem *item : asConst(m_itemList)) |
||||
size = size.expandedTo(item->minimumSize()); |
||||
|
||||
const QMargins margins = contentsMargins(); |
||||
size += QSize(margins.left() + margins.right(), margins.top() + margins.bottom()); |
||||
return size; |
||||
} |
||||
|
||||
int FlowLayout::doLayout(const QRect &rect, const bool testOnly) const |
||||
{ |
||||
int left, top, right, bottom; |
||||
getContentsMargins(&left, &top, &right, &bottom); |
||||
|
||||
const QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom); |
||||
int x = effectiveRect.x(); |
||||
int y = effectiveRect.y(); |
||||
int lineHeight = 0; |
||||
|
||||
for (QLayoutItem *item : asConst(m_itemList)) |
||||
{ |
||||
const QWidget *wid = item->widget(); |
||||
|
||||
int spaceX = horizontalSpacing(); |
||||
if (spaceX == -1) |
||||
{ |
||||
spaceX = wid->style()->layoutSpacing( |
||||
QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal); |
||||
} |
||||
|
||||
int spaceY = verticalSpacing(); |
||||
if (spaceY == -1) |
||||
{ |
||||
spaceY = wid->style()->layoutSpacing( |
||||
QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical); |
||||
} |
||||
|
||||
int nextX = x + item->sizeHint().width() + spaceX; |
||||
if (((nextX - spaceX) > effectiveRect.right()) && (lineHeight > 0)) |
||||
{ |
||||
x = effectiveRect.x(); |
||||
y = y + lineHeight + spaceY; |
||||
nextX = x + item->sizeHint().width() + spaceX; |
||||
lineHeight = 0; |
||||
} |
||||
|
||||
if (!testOnly) |
||||
item->setGeometry(QRect(QPoint(x, y), item->sizeHint())); |
||||
|
||||
x = nextX; |
||||
lineHeight = std::max(lineHeight, item->sizeHint().height()); |
||||
} |
||||
|
||||
return y + lineHeight - rect.y() + bottom; |
||||
} |
||||
|
||||
int FlowLayout::smartSpacing(const QStyle::PixelMetric pm) const |
||||
{ |
||||
QObject *parent = this->parent(); |
||||
if (!parent) |
||||
return -1; |
||||
|
||||
if (parent->isWidgetType()) |
||||
{ |
||||
auto *pw = static_cast<QWidget *>(parent); |
||||
return pw->style()->pixelMetric(pm, nullptr, pw); |
||||
} |
||||
|
||||
return static_cast<QLayout *>(parent)->spacing(); |
||||
} |
@ -0,0 +1,63 @@
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Bittorrent Client using Qt and libtorrent. |
||||
* Copyright (C) 2023 Vladimir Golovnev <glassez@yandex.ru> |
||||
* Copyright (C) 2016 The Qt Company Ltd. |
||||
* |
||||
* This program is free software; you can redistribute it and/or |
||||
* modify it under the terms of the GNU General Public License |
||||
* as published by the Free Software Foundation; either version 2 |
||||
* of the License, or (at your option) any later version. |
||||
* |
||||
* This program is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License |
||||
* along with this program; if not, write to the Free Software |
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||||
* |
||||
* In addition, as a special exception, the copyright holders give permission to |
||||
* link this program with the OpenSSL project's "OpenSSL" library (or with |
||||
* modified versions of it that use the same license as the "OpenSSL" library), |
||||
* and distribute the linked executables. You must obey the GNU General Public |
||||
* License in all respects for all of the code used other than "OpenSSL". If you |
||||
* modify file(s), you may extend this exception to your version of the file(s), |
||||
* but you are not obligated to do so. If you do not wish to do so, delete this |
||||
* exception statement from your version. |
||||
*/ |
||||
|
||||
#pragma once |
||||
|
||||
#include <QLayout> |
||||
#include <QRect> |
||||
#include <QStyle> |
||||
|
||||
class FlowLayout final : public QLayout |
||||
{ |
||||
public: |
||||
explicit FlowLayout(QWidget *parent, int margin = -1, int hSpacing = -1, int vSpacing = -1); |
||||
explicit FlowLayout(int margin = -1, int hSpacing = -1, int vSpacing = -1); |
||||
~FlowLayout() override; |
||||
|
||||
void addItem(QLayoutItem *item) override; |
||||
int horizontalSpacing() const; |
||||
int verticalSpacing() const; |
||||
Qt::Orientations expandingDirections() const override; |
||||
bool hasHeightForWidth() const override; |
||||
int heightForWidth(int) const override; |
||||
int count() const override; |
||||
QLayoutItem *itemAt(int index) const override; |
||||
QSize minimumSize() const override; |
||||
void setGeometry(const QRect &rect) override; |
||||
QSize sizeHint() const override; |
||||
QLayoutItem *takeAt(int index) override; |
||||
|
||||
private: |
||||
int doLayout(const QRect &rect, bool testOnly) const; |
||||
int smartSpacing(QStyle::PixelMetric pm) const; |
||||
|
||||
QList<QLayoutItem *> m_itemList; |
||||
int m_hSpace; |
||||
int m_vSpace; |
||||
}; |
@ -0,0 +1,121 @@
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* Bittorrent Client using Qt and libtorrent. |
||||
* Copyright (C) 2023 Vladimir Golovnev <glassez@yandex.ru> |
||||
* |
||||
* This program is free software; you can redistribute it and/or |
||||
* modify it under the terms of the GNU General Public License |
||||
* as published by the Free Software Foundation; either version 2 |
||||
* of the License, or (at your option) any later version. |
||||
* |
||||
* This program is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License |
||||
* along with this program; if not, write to the Free Software |
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||||
* |
||||
* In addition, as a special exception, the copyright holders give permission to |
||||
* link this program with the OpenSSL project's "OpenSSL" library (or with |
||||
* modified versions of it that use the same license as the "OpenSSL" library), |
||||
* and distribute the linked executables. You must obey the GNU General Public |
||||
* License in all respects for all of the code used other than "OpenSSL". If you |
||||
* modify file(s), you may extend this exception to your version of the file(s), |
||||
* but you are not obligated to do so. If you do not wish to do so, delete this |
||||
* exception statement from your version. |
||||
*/ |
||||
|
||||
#include "torrenttagsdialog.h" |
||||
|
||||
#include <QCheckBox> |
||||
#include <QMessageBox> |
||||
#include <QPushButton> |
||||
#include <QSet> |
||||
|
||||
#include "base/bittorrent/session.h" |
||||
#include "base/global.h" |
||||
#include "autoexpandabledialog.h" |
||||
#include "flowlayout.h" |
||||
|
||||
#include "ui_torrenttagsdialog.h" |
||||
|
||||
#define SETTINGS_KEY(name) u"GUI/TorrentTagsDialog/" name |
||||
|
||||
TorrentTagsDialog::TorrentTagsDialog(const TagSet &initialTags, QWidget *parent) |
||||
: QDialog(parent) |
||||
, m_ui {new Ui::TorrentTagsDialog} |
||||
, m_storeDialogSize {SETTINGS_KEY(u"Size"_qs)} |
||||
{ |
||||
m_ui->setupUi(this); |
||||
|
||||
auto *tagsLayout = new FlowLayout(m_ui->scrollArea); |
||||
for (const QString &tag : asConst(initialTags.united(BitTorrent::Session::instance()->tags()))) |
||||
{ |
||||
auto *tagWidget = new QCheckBox(tag); |
||||
if (initialTags.contains(tag)) |
||||
tagWidget->setChecked(true); |
||||
tagsLayout->addWidget(tagWidget); |
||||
} |
||||
|
||||
auto *addTagButton = new QPushButton(u"+"_qs); |
||||
connect(addTagButton, &QPushButton::clicked, this, &TorrentTagsDialog::addNewTag); |
||||
tagsLayout->addWidget(addTagButton); |
||||
|
||||
if (const QSize dialogSize = m_storeDialogSize; dialogSize.isValid()) |
||||
resize(dialogSize); |
||||
} |
||||
|
||||
TorrentTagsDialog::~TorrentTagsDialog() |
||||
{ |
||||
m_storeDialogSize = size(); |
||||
delete m_ui; |
||||
} |
||||
|
||||
TagSet TorrentTagsDialog::tags() const |
||||
{ |
||||
TagSet tags; |
||||
auto *layout = m_ui->scrollArea->layout(); |
||||
for (int i = 0; i < (layout->count() - 1); ++i) |
||||
{ |
||||
const auto *tagWidget = static_cast<QCheckBox *>(layout->itemAt(i)->widget()); |
||||
if (tagWidget->isChecked()) |
||||
tags.insert(tagWidget->text()); |
||||
} |
||||
|
||||
return tags; |
||||
} |
||||
|
||||
void TorrentTagsDialog::addNewTag() |
||||
{ |
||||
bool done = false; |
||||
QString tag; |
||||
while (!done) |
||||
{ |
||||
bool ok = false; |
||||
tag = AutoExpandableDialog::getText(this |
||||
, tr("New Tag"), tr("Tag:"), QLineEdit::Normal, tag, &ok).trimmed(); |
||||
if (!ok || tag.isEmpty()) |
||||
break; |
||||
|
||||
if (!BitTorrent::Session::isValidTag(tag)) |
||||
{ |
||||
QMessageBox::warning(this, tr("Invalid tag name"), tr("Tag name '%1' is invalid.").arg(tag)); |
||||
} |
||||
else if (BitTorrent::Session::instance()->tags().contains(tag)) |
||||
{ |
||||
QMessageBox::warning(this, tr("Tag exists"), tr("Tag name already exists.")); |
||||
} |
||||
else |
||||
{ |
||||
auto *layout = m_ui->scrollArea->layout(); |
||||
auto *btn = layout->takeAt(layout->count() - 1); |
||||
auto *tagWidget = new QCheckBox(tag); |
||||
tagWidget->setChecked(true); |
||||
layout->addWidget(tagWidget); |
||||
layout->addItem(btn); |
||||
|
||||
done = true; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,57 @@
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Bittorrent Client using Qt and libtorrent. |
||||
* Copyright (C) 2023 Vladimir Golovnev <glassez@yandex.ru> |
||||
* |
||||
* This program is free software; you can redistribute it and/or |
||||
* modify it under the terms of the GNU General Public License |
||||
* as published by the Free Software Foundation; either version 2 |
||||
* of the License, or (at your option) any later version. |
||||
* |
||||
* This program is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License |
||||
* along with this program; if not, write to the Free Software |
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||||
* |
||||
* In addition, as a special exception, the copyright holders give permission to |
||||
* link this program with the OpenSSL project's "OpenSSL" library (or with |
||||
* modified versions of it that use the same license as the "OpenSSL" library), |
||||
* and distribute the linked executables. You must obey the GNU General Public |
||||
* License in all respects for all of the code used other than "OpenSSL". If you |
||||
* modify file(s), you may extend this exception to your version of the file(s), |
||||
* but you are not obligated to do so. If you do not wish to do so, delete this |
||||
* exception statement from your version. |
||||
*/ |
||||
|
||||
#pragma once |
||||
|
||||
#include <QDialog> |
||||
|
||||
#include "base/settingvalue.h" |
||||
#include "base/tagset.h" |
||||
|
||||
namespace Ui |
||||
{ |
||||
class TorrentTagsDialog; |
||||
} |
||||
|
||||
class TorrentTagsDialog final : public QDialog |
||||
{ |
||||
Q_OBJECT |
||||
Q_DISABLE_COPY_MOVE(TorrentTagsDialog) |
||||
|
||||
public: |
||||
explicit TorrentTagsDialog(const TagSet &initialTags, QWidget *parent = nullptr); |
||||
~TorrentTagsDialog() override; |
||||
|
||||
TagSet tags() const; |
||||
|
||||
private: |
||||
void addNewTag(); |
||||
|
||||
Ui::TorrentTagsDialog *m_ui; |
||||
SettingValue<QSize> m_storeDialogSize; |
||||
}; |
@ -0,0 +1,81 @@
@@ -0,0 +1,81 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<ui version="4.0"> |
||||
<class>TorrentTagsDialog</class> |
||||
<widget class="QDialog" name="TorrentTagsDialog"> |
||||
<property name="geometry"> |
||||
<rect> |
||||
<x>0</x> |
||||
<y>0</y> |
||||
<width>484</width> |
||||
<height>313</height> |
||||
</rect> |
||||
</property> |
||||
<property name="windowTitle"> |
||||
<string>Torrent Tags</string> |
||||
</property> |
||||
<layout class="QVBoxLayout" name="verticalLayout"> |
||||
<item> |
||||
<widget class="QScrollArea" name="scrollArea"> |
||||
<property name="horizontalScrollBarPolicy"> |
||||
<enum>Qt::ScrollBarAlwaysOff</enum> |
||||
</property> |
||||
<property name="widgetResizable"> |
||||
<bool>true</bool> |
||||
</property> |
||||
<widget class="QWidget" name="scrollAreaWidgetContents"> |
||||
<property name="geometry"> |
||||
<rect> |
||||
<x>0</x> |
||||
<y>0</y> |
||||
<width>464</width> |
||||
<height>263</height> |
||||
</rect> |
||||
</property> |
||||
</widget> |
||||
</widget> |
||||
</item> |
||||
<item> |
||||
<widget class="QDialogButtonBox" name="buttonBox"> |
||||
<property name="standardButtons"> |
||||
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set> |
||||
</property> |
||||
</widget> |
||||
</item> |
||||
</layout> |
||||
</widget> |
||||
<resources/> |
||||
<connections> |
||||
<connection> |
||||
<sender>buttonBox</sender> |
||||
<signal>accepted()</signal> |
||||
<receiver>TorrentTagsDialog</receiver> |
||||
<slot>accept()</slot> |
||||
<hints> |
||||
<hint type="sourcelabel"> |
||||
<x>241</x> |
||||
<y>291</y> |
||||
</hint> |
||||
<hint type="destinationlabel"> |
||||
<x>241</x> |
||||
<y>156</y> |
||||
</hint> |
||||
</hints> |
||||
</connection> |
||||
<connection> |
||||
<sender>buttonBox</sender> |
||||
<signal>rejected()</signal> |
||||
<receiver>TorrentTagsDialog</receiver> |
||||
<slot>reject()</slot> |
||||
<hints> |
||||
<hint type="sourcelabel"> |
||||
<x>241</x> |
||||
<y>291</y> |
||||
</hint> |
||||
<hint type="destinationlabel"> |
||||
<x>241</x> |
||||
<y>156</y> |
||||
</hint> |
||||
</hints> |
||||
</connection> |
||||
</connections> |
||||
</ui> |
Loading…
Reference in new issue