4 #include "librarieseditor.h" |
4 #include "librarieseditor.h" |
5 #include "ui_librarieseditor.h" |
5 #include "ui_librarieseditor.h" |
6 |
6 |
7 LibrariesEditor::LibrariesEditor(QWidget* parent) : |
7 LibrariesEditor::LibrariesEditor(QWidget* parent) : |
8 QWidget{parent}, |
8 QWidget{parent}, |
9 libraries{this}, |
|
10 ui{*new Ui_LibrariesEditor} |
9 ui{*new Ui_LibrariesEditor} |
11 { |
10 { |
12 this->ui.setupUi(this); |
11 this->ui.setupUi(this); |
13 connect( |
12 connect( |
14 this->ui.newLibrarySearch, |
13 this->ui.newLibrarySearch, |
18 connect( |
17 connect( |
19 this->ui.newLibraryAdd, |
18 this->ui.newLibraryAdd, |
20 &QPushButton::clicked, |
19 &QPushButton::clicked, |
21 this, |
20 this, |
22 &LibrariesEditor::addNewLibrary); |
21 &LibrariesEditor::addNewLibrary); |
23 this->ui.librariesTable->setModel(&this->libraries); |
|
24 this->ui.librariesTable->setContextMenuPolicy(Qt::CustomContextMenu); |
22 this->ui.librariesTable->setContextMenuPolicy(Qt::CustomContextMenu); |
25 connect( |
23 connect( |
26 this->ui.librariesTable, |
24 this->ui.librariesTable, |
27 &QWidget::customContextMenuRequested, |
25 &QWidget::customContextMenuRequested, |
28 this, |
26 this, |
29 &LibrariesEditor::showContextMenu); |
27 &LibrariesEditor::showContextMenu); |
|
28 this->setEnabled(false); |
30 } |
29 } |
31 |
30 |
32 LibrariesEditor::~LibrariesEditor() |
31 LibrariesEditor::~LibrariesEditor() |
33 { |
32 { |
34 delete &this->ui; |
33 delete &this->ui; |
43 } |
42 } |
44 } |
43 } |
45 |
44 |
46 void LibrariesEditor::addNewLibrary() |
45 void LibrariesEditor::addNewLibrary() |
47 { |
46 { |
48 const QDir dir{this->ui.newLibraryPath->text()}; |
47 if (LibrariesModel* model = this->currentModel()) { |
49 if (not dir.exists()) |
48 const QDir dir{this->ui.newLibraryPath->text()}; |
50 { |
49 if (not dir.exists()) |
51 QMessageBox::critical( |
50 { |
|
51 QMessageBox::critical( |
52 this, |
52 this, |
53 tr("Library does not exist"), |
53 tr("Library does not exist"), |
54 tr("The directory %1 does not exist.").arg(quoted(dir.path())) |
54 tr("The directory %1 does not exist.").arg(quoted(dir.path())) |
55 ); |
55 ); |
56 } |
56 } |
57 else |
57 else |
58 { |
|
59 if (not dir.isReadable()) |
|
60 { |
58 { |
61 QMessageBox::warning(this, |
59 if (not dir.isReadable()) |
|
60 { |
|
61 QMessageBox::warning(this, |
62 tr("Unreadable library"), |
62 tr("Unreadable library"), |
63 tr("The directory %1 cannot be read.").arg(quoted(dir.path())) |
63 tr("The directory %1 cannot be read.").arg(quoted(dir.path())) |
64 ); |
64 ); |
|
65 } |
|
66 model->addLibrary({Library::OfficialLibrary, dir}); |
|
67 this->ui.newLibraryPath->clear(); |
65 } |
68 } |
66 this->libraries.addLibrary({Library::OfficialLibrary, dir}); |
|
67 this->ui.newLibraryPath->clear(); |
|
68 } |
69 } |
69 } |
70 } |
70 |
71 |
71 void LibrariesEditor::showContextMenu(const QPoint position) |
72 void LibrariesEditor::showContextMenu(const QPoint position) |
72 { |
73 { |
73 const int libraryIndex = this->currentLibraryIndex(); |
74 const std::size_t libraryIndex = this->currentLibraryIndex(); |
74 if (this->libraries.isValidIndex(libraryIndex)) |
75 LibrariesModel* model = this->currentModel(); |
|
76 if (model != nullptr and model->isValidIndex(libraryIndex)) |
75 { |
77 { |
76 QMenu* contextMenu = new QMenu{this}; |
78 QMenu* contextMenu = new QMenu{this}; |
77 QAction* removeAction = new QAction{tr("Remove library")}; |
79 QAction* removeAction = new QAction{tr("Remove library")}; |
78 connect( |
80 connect( |
79 removeAction, |
81 removeAction, |
113 } |
115 } |
114 } |
116 } |
115 |
117 |
116 void LibrariesEditor::setCurrentLibraryRole() |
118 void LibrariesEditor::setCurrentLibraryRole() |
117 { |
119 { |
118 const int libraryIndex = currentLibraryIndex(); |
120 if (LibrariesModel* model = this->currentModel()) { |
119 QObject* senderObject = sender(); |
121 const std::size_t libraryIndex = currentLibraryIndex(); |
120 QAction* senderAction = qobject_cast<QAction*>(senderObject); |
122 QObject* senderObject = sender(); |
121 const Library::Role role = senderAction->data().value<Library::Role>(); |
123 QAction* senderAction = qobject_cast<QAction*>(senderObject); |
122 this->libraries.setLibraryRole(libraryIndex, role); |
124 const Library::Role role = senderAction->data().value<Library::Role>(); |
|
125 model->setLibraryRole(libraryIndex, role); |
|
126 } |
123 } |
127 } |
124 |
128 |
125 void LibrariesEditor::removeCurrentLibrary() |
129 void LibrariesEditor::removeCurrentLibrary() |
126 { |
130 { |
127 this->libraries.removeLibrary(currentLibraryIndex()); |
131 if (LibrariesModel* model = this->currentModel()) { |
|
132 model->removeLibrary(currentLibraryIndex()); |
|
133 } |
128 } |
134 } |
129 |
135 |
130 void LibrariesEditor::moveCurrentLibraryUp() |
136 void LibrariesEditor::moveCurrentLibraryUp() |
131 { |
137 { |
132 const int libraryIndex = this->currentLibraryIndex(); |
138 if (LibrariesModel* model = this->currentModel()) { |
133 this->libraries.moveLibrary(libraryIndex, libraryIndex - 1); |
139 const std::size_t libraryIndex = this->currentLibraryIndex(); |
|
140 model->moveLibrary(libraryIndex, libraryIndex - 1); |
|
141 } |
134 } |
142 } |
135 |
143 |
136 void LibrariesEditor::moveCurrentLibraryDown() |
144 void LibrariesEditor::moveCurrentLibraryDown() |
137 { |
145 { |
138 const int libraryIndex = this->currentLibraryIndex(); |
146 if (LibrariesModel* model = this->currentModel()) { |
139 this->libraries.moveLibrary(libraryIndex + 1, libraryIndex); |
147 const std::size_t libraryIndex = this->currentLibraryIndex(); |
|
148 model->moveLibrary(libraryIndex + 1, libraryIndex); |
|
149 } |
140 } |
150 } |
141 |
151 |
142 int LibrariesEditor::currentLibraryIndex() const |
152 LibrariesModel *LibrariesEditor::currentModel() |
143 { |
153 { |
144 return this->ui.librariesTable->selectionModel()->currentIndex().row(); |
154 return qobject_cast<LibrariesModel*>(this->ui.librariesTable->model()); |
|
155 } |
|
156 |
|
157 std::size_t LibrariesEditor::currentLibraryIndex() const |
|
158 { |
|
159 const int row = this->ui.librariesTable->selectionModel()->currentIndex().row(); |
|
160 return static_cast<std::size_t>(row); |
145 } |
161 } |
146 |
162 |
147 void LibrariesEditor::saveSettings() |
163 void LibrariesEditor::saveSettings() |
148 { |
164 { |
149 this->libraries.storeToSettings(); |
165 if (LibrariesModel* model = this->currentModel()) { |
|
166 model->storeToSettings(); |
|
167 } |
150 } |
168 } |
|
169 |
|
170 void LibrariesEditor::setModel(LibrariesModel *model) |
|
171 { |
|
172 this->ui.librariesTable->setModel(model); |
|
173 this->setEnabled(model != nullptr); |
|
174 } |