Mercurial > minori
annotate src/gui/dialog/settings/library.cc @ 236:4d461ef7d424
HUGE UPDATE: convert build system to autotools
why? because cmake sucks :)
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Fri, 19 Jan 2024 00:24:02 -0500 |
| parents | ff0061e75f0f |
| children | 862d0d8619f6 |
| 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 DroppableListWidget::DroppableListWidget(QWidget* parent) : QListWidget(parent) { | |
| 23 setAcceptDrops(true); | |
| 24 } | |
| 25 | |
| 26 void DroppableListWidget::dragMoveEvent(QDragMoveEvent* event) { | |
| 27 if (event->mimeData()->hasUrls()) | |
| 28 event->acceptProposedAction(); | |
| 29 } | |
| 30 | |
| 31 void DroppableListWidget::dragEnterEvent(QDragEnterEvent* event) { | |
| 32 if (event->mimeData()->hasUrls()) | |
| 33 event->acceptProposedAction(); | |
| 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 | |
| 88 connect(listwidget, &DroppableListWidget::FilesDropped, this, [this, listwidget](QStringList list){ | |
| 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 | |
| 112 connect(button, &QPushButton::clicked, this, [this, listwidget]{ | |
| 113 const QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"), | |
| 114 QDir::homePath(), | |
| 115 QFileDialog::ShowDirsOnly | |
| 116 | QFileDialog::DontResolveSymlinks); | |
|
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
232
diff
changeset
|
117 const std::string s_dir = Strings::ToUtf8String(dir); |
|
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
232
diff
changeset
|
118 if (dir.isEmpty() || paths.count(s_dir)) |
| 226 | 119 return; |
|
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
232
diff
changeset
|
120 |
|
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
232
diff
changeset
|
121 paths.insert(s_dir); |
| 226 | 122 QListWidgetItem* item = new QListWidgetItem(listwidget); |
| 123 item->setText(dir); | |
| 124 }); | |
| 125 | |
| 126 widget_layout->addWidget(button); | |
| 127 } | |
| 128 | |
| 129 { | |
| 130 QPushButton* button = new QPushButton(tr("Remove"), widget); | |
| 131 | |
| 132 connect(listwidget, &QListWidget::itemSelectionChanged, this, [button, listwidget]{ | |
| 133 QList<QListWidgetItem*> selection = listwidget->selectedItems(); | |
| 134 button->setEnabled(selection.size() > 0); | |
| 135 }); | |
| 136 | |
| 137 connect(button, &QPushButton::clicked, this, [this, listwidget]{ | |
| 138 QList<QListWidgetItem*> selection = listwidget->selectedItems(); | |
| 139 for (const auto& item : selection) { | |
| 140 paths.erase(Strings::ToUtf8String(item->text())); | |
| 141 delete item; | |
| 142 } | |
| 143 }); | |
| 144 | |
| 145 widget_layout->addWidget(button); | |
| 146 } | |
| 147 | |
| 148 group_box_layout->addWidget(widget); | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 full_layout->addWidget(group_box); | |
| 153 } | |
| 154 | |
| 155 { | |
| 156 QGroupBox* group_box = new QGroupBox(tr("Real-time monitor"), result); | |
| 157 group_box->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum); | |
| 158 | |
| 159 QVBoxLayout* group_box_layout = new QVBoxLayout(group_box); | |
| 160 | |
| 161 { | |
| 162 QCheckBox* checkbox = new QCheckBox(tr("Detect new files and folders under library folders"), group_box); | |
| 163 checkbox->setCheckState(real_time_monitor ? Qt::Checked : Qt::Unchecked); | |
| 164 | |
| 165 connect(checkbox, &QCheckBox::stateChanged, this, [this](int state) { | |
| 166 real_time_monitor = (state != Qt::Unchecked); | |
| 167 }); | |
| 168 | |
| 169 group_box_layout->addWidget(checkbox); | |
| 170 } | |
| 171 | |
| 172 full_layout->addWidget(group_box); | |
| 173 } | |
| 174 | |
| 175 full_layout->setSpacing(10); | |
| 176 full_layout->addStretch(); | |
| 177 | |
| 178 return result; | |
| 179 } | |
| 180 | |
| 181 void SettingsPageLibrary::SaveInfo() { | |
| 182 session.config.library.paths = paths; | |
| 183 session.config.library.real_time_monitor = real_time_monitor; | |
| 184 } | |
| 185 | |
| 186 SettingsPageLibrary::SettingsPageLibrary(QWidget* parent) | |
| 187 : SettingsPage(parent, tr("Library")), | |
| 188 paths(session.config.library.paths) { | |
| 189 real_time_monitor = session.config.library.real_time_monitor; | |
| 190 AddTab(CreateFoldersWidget(), tr("Folder")); | |
| 191 } |
