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