Mercurial > minori
comparison src/gui/dialog/settings/library.cc @ 226:f784b5b1914c
settings: add library page
| author | Paper <mrpapersonic@gmail.com> | 
|---|---|
| date | Mon, 08 Jan 2024 21:23:00 -0500 | 
| parents | |
| children | d030b30526d5 | 
   comparison
  equal
  deleted
  inserted
  replaced
| 225:56ea2bdc6724 | 226:f784b5b1914c | 
|---|---|
| 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) { | |
| 52 const QMimeData *mime_data = event->mimeData(); | |
| 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->setAutoFillBackground(true); | |
| 79 result->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum); | |
| 80 | |
| 81 QVBoxLayout* full_layout = new QVBoxLayout(result); | |
| 82 | |
| 83 { | |
| 84 QGroupBox* group_box = new QGroupBox(tr("Library folders"), result); | |
| 85 group_box->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum); | |
| 86 | |
| 87 QVBoxLayout* group_box_layout = new QVBoxLayout(group_box); | |
| 88 | |
| 89 { | |
| 90 QLabel* label = new QLabel(tr("These folders will be scanned and monitored for new episodes."), group_box); | |
| 91 group_box_layout->addWidget(label); | |
| 92 } | |
| 93 | |
| 94 { | |
| 95 DroppableListWidget* listwidget = new DroppableListWidget(group_box); | |
| 96 listwidget->setSelectionMode(QAbstractItemView::ExtendedSelection); | |
| 97 | |
| 98 for (const auto& path : paths) { | |
| 99 QListWidgetItem* item = new QListWidgetItem(listwidget); | |
| 100 item->setText(Strings::ToQString(path)); | |
| 101 /* add icons as well soon */ | |
| 102 } | |
| 103 | |
| 104 connect(listwidget, &DroppableListWidget::FilesDropped, this, [this, listwidget](QStringList list){ | |
| 105 for (const auto& dir : list) { | |
| 106 paths.insert(Strings::ToUtf8String(dir)); | |
| 107 QListWidgetItem* item = new QListWidgetItem(listwidget); | |
| 108 item->setText(dir); | |
| 109 } | |
| 110 }); | |
| 111 | |
| 112 group_box_layout->addWidget(listwidget); | |
| 113 | |
| 114 { | |
| 115 QWidget* widget = new QWidget(group_box); | |
| 116 QHBoxLayout* widget_layout = new QHBoxLayout(widget); | |
| 117 | |
| 118 { | |
| 119 QLabel* label = new QLabel(tr("Tip: You can drag and drop folders here."), widget); | |
| 120 widget_layout->addWidget(label); | |
| 121 } | |
| 122 | |
| 123 widget_layout->addStretch(); | |
| 124 | |
| 125 { | |
| 126 QPushButton* button = new QPushButton(tr("Add new..."), widget); | |
| 127 | |
| 128 connect(button, &QPushButton::clicked, this, [this, listwidget]{ | |
| 129 const QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"), | |
| 130 QDir::homePath(), | |
| 131 QFileDialog::ShowDirsOnly | |
| 132 | QFileDialog::DontResolveSymlinks); | |
| 133 if (dir.isEmpty()) | |
| 134 return; | |
| 135 paths.insert(Strings::ToUtf8String(dir)); | |
| 136 QListWidgetItem* item = new QListWidgetItem(listwidget); | |
| 137 item->setText(dir); | |
| 138 }); | |
| 139 | |
| 140 widget_layout->addWidget(button); | |
| 141 } | |
| 142 | |
| 143 { | |
| 144 QPushButton* button = new QPushButton(tr("Remove"), widget); | |
| 145 | |
| 146 connect(listwidget, &QListWidget::itemSelectionChanged, this, [button, listwidget]{ | |
| 147 QList<QListWidgetItem*> selection = listwidget->selectedItems(); | |
| 148 button->setEnabled(selection.size() > 0); | |
| 149 }); | |
| 150 | |
| 151 connect(button, &QPushButton::clicked, this, [this, listwidget]{ | |
| 152 QList<QListWidgetItem*> selection = listwidget->selectedItems(); | |
| 153 for (const auto& item : selection) { | |
| 154 paths.erase(Strings::ToUtf8String(item->text())); | |
| 155 delete item; | |
| 156 } | |
| 157 }); | |
| 158 | |
| 159 widget_layout->addWidget(button); | |
| 160 } | |
| 161 | |
| 162 group_box_layout->addWidget(widget); | |
| 163 } | |
| 164 } | |
| 165 | |
| 166 full_layout->addWidget(group_box); | |
| 167 } | |
| 168 | |
| 169 { | |
| 170 QGroupBox* group_box = new QGroupBox(tr("Real-time monitor"), result); | |
| 171 group_box->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum); | |
| 172 | |
| 173 QVBoxLayout* group_box_layout = new QVBoxLayout(group_box); | |
| 174 | |
| 175 { | |
| 176 QCheckBox* checkbox = new QCheckBox(tr("Detect new files and folders under library folders"), group_box); | |
| 177 checkbox->setCheckState(real_time_monitor ? Qt::Checked : Qt::Unchecked); | |
| 178 | |
| 179 connect(checkbox, &QCheckBox::stateChanged, this, [this](int state) { | |
| 180 real_time_monitor = (state != Qt::Unchecked); | |
| 181 }); | |
| 182 | |
| 183 group_box_layout->addWidget(checkbox); | |
| 184 } | |
| 185 | |
| 186 full_layout->addWidget(group_box); | |
| 187 } | |
| 188 | |
| 189 full_layout->setSpacing(10); | |
| 190 full_layout->addStretch(); | |
| 191 | |
| 192 return result; | |
| 193 } | |
| 194 | |
| 195 void SettingsPageLibrary::SaveInfo() { | |
| 196 session.config.library.paths = paths; | |
| 197 session.config.library.real_time_monitor = real_time_monitor; | |
| 198 } | |
| 199 | |
| 200 SettingsPageLibrary::SettingsPageLibrary(QWidget* parent) | |
| 201 : SettingsPage(parent, tr("Library")), | |
| 202 paths(session.config.library.paths) { | |
| 203 real_time_monitor = session.config.library.real_time_monitor; | |
| 204 AddTab(CreateFoldersWidget(), tr("Folder")); | |
| 205 } | |
| 206 | |
| 207 #include "gui/dialog/settings/library.moc" | 
