Mercurial > minori
annotate src/gui/dialog/settings/library.cc @ 316:180714442770
settings: clean up code
| author | Paper <paper@paper.us.eu.org> |
|---|---|
| date | Tue, 11 Jun 2024 15:11:09 -0400 |
| parents | 862d0d8619f6 |
| children | b1f4d1867ab1 |
| rev | line source |
|---|---|
| 226 | 1 #include "core/session.h" |
| 2 #include "core/strings.h" | |
| 3 #include "gui/dialog/settings.h" | |
| 4 | |
| 258 | 5 #include <QCheckBox> |
| 6 #include <QDir> | |
| 7 #include <QDropEvent> | |
| 8 #include <QFileDialog> | |
| 9 #include <QFileInfo> | |
| 10 #include <QGroupBox> | |
| 11 #include <QLabel> | |
| 226 | 12 #include <QListWidget> |
| 13 #include <QListWidgetItem> | |
| 258 | 14 #include <QMimeData> |
| 15 #include <QPushButton> | |
| 226 | 16 #include <QSizePolicy> |
| 17 #include <QVBoxLayout> | |
| 18 | |
| 19 #include <algorithm> | |
| 20 #include <iostream> | |
| 21 | |
| 22 DroppableListWidget::DroppableListWidget(QWidget* parent) : QListWidget(parent) { | |
| 23 setAcceptDrops(true); | |
| 24 } | |
| 25 | |
| 26 void DroppableListWidget::dragMoveEvent(QDragMoveEvent* event) { | |
| 258 | 27 if (event->mimeData()->hasUrls()) |
| 28 event->acceptProposedAction(); | |
| 226 | 29 } |
| 30 | |
| 31 void DroppableListWidget::dragEnterEvent(QDragEnterEvent* event) { | |
| 258 | 32 if (event->mimeData()->hasUrls()) |
| 33 event->acceptProposedAction(); | |
| 226 | 34 } |
| 35 | |
| 36 void DroppableListWidget::dropEvent(QDropEvent* event) { | |
|
228
d030b30526d5
config: remove unused username parameter from anilist auth
Paper <mrpapersonic@gmail.com>
parents:
226
diff
changeset
|
37 const QMimeData* mime_data = event->mimeData(); |
| 226 | 38 |
| 39 if (!mime_data->hasUrls()) | |
| 40 return; | |
| 41 | |
| 42 QStringList path_list; | |
| 43 QList<QUrl> url_list = mime_data->urls(); | |
| 44 | |
| 45 for (const auto& url : url_list) { | |
| 46 if (!url.isLocalFile()) | |
| 47 continue; | |
| 48 | |
| 49 const QString file = url.toLocalFile(); | |
| 50 const QFileInfo fileinfo(file); | |
| 51 if (fileinfo.exists() && fileinfo.isDir()) | |
| 52 path_list.append(file); | |
| 53 } | |
| 54 | |
| 55 if (!path_list.isEmpty()) | |
| 56 emit FilesDropped(path_list); | |
| 57 | |
| 58 event->acceptProposedAction(); | |
| 59 } | |
| 60 | |
| 61 QWidget* SettingsPageLibrary::CreateFoldersWidget() { | |
| 62 QWidget* result = new QWidget(this); | |
| 63 result->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum); | |
| 64 | |
| 65 QVBoxLayout* full_layout = new QVBoxLayout(result); | |
| 66 | |
| 67 { | |
| 68 QGroupBox* group_box = new QGroupBox(tr("Library folders"), result); | |
| 69 group_box->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum); | |
| 70 | |
| 71 QVBoxLayout* group_box_layout = new QVBoxLayout(group_box); | |
| 72 | |
| 73 { | |
| 74 QLabel* label = new QLabel(tr("These folders will be scanned and monitored for new episodes."), group_box); | |
| 75 group_box_layout->addWidget(label); | |
| 76 } | |
| 77 | |
| 78 { | |
| 79 DroppableListWidget* listwidget = new DroppableListWidget(group_box); | |
| 80 listwidget->setSelectionMode(QAbstractItemView::ExtendedSelection); | |
| 81 | |
| 82 for (const auto& path : paths) { | |
| 83 QListWidgetItem* item = new QListWidgetItem(listwidget); | |
| 84 item->setText(Strings::ToQString(path)); | |
| 85 /* add icons as well soon */ | |
| 86 } | |
| 87 | |
| 258 | 88 connect(listwidget, &DroppableListWidget::FilesDropped, this, [this, listwidget](QStringList list) { |
| 226 | 89 for (const auto& dir : list) { |
| 90 paths.insert(Strings::ToUtf8String(dir)); | |
| 91 QListWidgetItem* item = new QListWidgetItem(listwidget); | |
| 92 item->setText(dir); | |
| 93 } | |
| 94 }); | |
| 95 | |
| 96 group_box_layout->addWidget(listwidget); | |
| 97 | |
| 98 { | |
| 99 QWidget* widget = new QWidget(group_box); | |
| 100 QHBoxLayout* widget_layout = new QHBoxLayout(widget); | |
| 101 | |
| 102 { | |
| 103 QLabel* label = new QLabel(tr("Tip: You can drag and drop folders here."), widget); | |
| 104 widget_layout->addWidget(label); | |
| 105 } | |
| 106 | |
| 107 widget_layout->addStretch(); | |
| 108 | |
| 109 { | |
| 110 QPushButton* button = new QPushButton(tr("Add new..."), widget); | |
| 111 | |
| 258 | 112 connect(button, &QPushButton::clicked, this, [this, listwidget] { |
| 113 const QString dir = QFileDialog::getExistingDirectory( | |
| 114 this, tr("Open Directory"), QDir::homePath(), | |
| 115 QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); | |
|
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
232
diff
changeset
|
116 const std::string s_dir = Strings::ToUtf8String(dir); |
|
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
232
diff
changeset
|
117 if (dir.isEmpty() || paths.count(s_dir)) |
| 226 | 118 return; |
|
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
232
diff
changeset
|
119 |
|
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
232
diff
changeset
|
120 paths.insert(s_dir); |
| 226 | 121 QListWidgetItem* item = new QListWidgetItem(listwidget); |
| 122 item->setText(dir); | |
| 123 }); | |
| 124 | |
| 125 widget_layout->addWidget(button); | |
| 126 } | |
| 127 | |
| 128 { | |
| 129 QPushButton* button = new QPushButton(tr("Remove"), widget); | |
| 130 | |
| 258 | 131 connect(listwidget, &QListWidget::itemSelectionChanged, this, [button, listwidget] { |
| 226 | 132 QList<QListWidgetItem*> selection = listwidget->selectedItems(); |
| 133 button->setEnabled(selection.size() > 0); | |
| 134 }); | |
| 135 | |
| 258 | 136 connect(button, &QPushButton::clicked, this, [this, listwidget] { |
| 226 | 137 QList<QListWidgetItem*> selection = listwidget->selectedItems(); |
| 138 for (const auto& item : selection) { | |
| 139 paths.erase(Strings::ToUtf8String(item->text())); | |
| 140 delete item; | |
| 141 } | |
| 142 }); | |
| 143 | |
| 144 widget_layout->addWidget(button); | |
| 145 } | |
| 146 | |
| 147 group_box_layout->addWidget(widget); | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 full_layout->addWidget(group_box); | |
| 152 } | |
| 153 | |
| 154 { | |
| 155 QGroupBox* group_box = new QGroupBox(tr("Real-time monitor"), result); | |
| 156 group_box->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum); | |
| 157 | |
| 158 QVBoxLayout* group_box_layout = new QVBoxLayout(group_box); | |
| 159 | |
| 160 { | |
| 161 QCheckBox* checkbox = new QCheckBox(tr("Detect new files and folders under library folders"), group_box); | |
| 162 checkbox->setCheckState(real_time_monitor ? Qt::Checked : Qt::Unchecked); | |
| 163 | |
| 258 | 164 connect(checkbox, &QCheckBox::stateChanged, this, |
| 165 [this](int state) { real_time_monitor = (state != Qt::Unchecked); }); | |
| 226 | 166 |
| 167 group_box_layout->addWidget(checkbox); | |
| 168 } | |
| 169 | |
| 170 full_layout->addWidget(group_box); | |
| 171 } | |
| 172 | |
| 173 full_layout->setSpacing(10); | |
| 174 full_layout->addStretch(); | |
| 175 | |
| 176 return result; | |
| 177 } | |
| 178 | |
| 179 void SettingsPageLibrary::SaveInfo() { | |
| 180 session.config.library.paths = paths; | |
| 181 session.config.library.real_time_monitor = real_time_monitor; | |
| 182 } | |
| 183 | |
| 184 SettingsPageLibrary::SettingsPageLibrary(QWidget* parent) | |
| 258 | 185 : SettingsPage(parent, tr("Library")), paths(session.config.library.paths) { |
| 226 | 186 real_time_monitor = session.config.library.real_time_monitor; |
| 187 AddTab(CreateFoldersWidget(), tr("Folder")); | |
| 188 } |
