Tue, 28 Jun 2022 13:03:21 +0300
Don't create more than one settings editor
7 | 1 | #include <QFileDialog> |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
2 | #include <QMenu> |
7 | 3 | #include <QMessageBox> |
264
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
4 | #include <ui_librarieseditor.h> |
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
5 | #include "src/settingseditor/librarieseditor.h" |
7 | 6 | |
218
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
7 | LibrariesEditor::LibrariesEditor(QWidget* parent) : |
7 | 8 | QWidget{parent}, |
9 | ui{*new Ui_LibrariesEditor} | |
10 | { | |
11 | this->ui.setupUi(this); | |
12 | connect( | |
13 | this->ui.newLibrarySearch, | |
14 | &QPushButton::clicked, | |
15 | this, | |
16 | &LibrariesEditor::searchPathForNewLibrary); | |
17 | connect( | |
18 | this->ui.newLibraryAdd, | |
19 | &QPushButton::clicked, | |
20 | this, | |
21 | &LibrariesEditor::addNewLibrary); | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
22 | this->ui.librariesTable->setContextMenuPolicy(Qt::CustomContextMenu); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
23 | connect( |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
24 | this->ui.librariesTable, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
25 | &QWidget::customContextMenuRequested, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
26 | this, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
27 | &LibrariesEditor::showContextMenu); |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
28 | this->setEnabled(false); |
7 | 29 | } |
30 | ||
31 | LibrariesEditor::~LibrariesEditor() | |
32 | { | |
33 | delete &this->ui; | |
34 | } | |
35 | ||
36 | void LibrariesEditor::searchPathForNewLibrary() | |
37 | { | |
38 | const QString path = QFileDialog::getExistingDirectory(this, tr("Browse LDraw library")); | |
39 | if (not path.isEmpty()) | |
40 | { | |
41 | this->ui.newLibraryPath->setText(path); | |
42 | } | |
43 | } | |
44 | ||
45 | void LibrariesEditor::addNewLibrary() | |
46 | { | |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
47 | if (LibrariesModel* model = this->currentModel()) { |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
48 | const QDir dir{this->ui.newLibraryPath->text()}; |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
49 | if (not dir.exists()) |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
50 | { |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
51 | QMessageBox::critical( |
206
654661eab7f3
More refactor, merged main.h, basics.h and utility.h into one header file basics.h and removed plenty of unused code
Teemu Piippo <teemu@hecknology.net>
parents:
201
diff
changeset
|
52 | this, |
7 | 53 | tr("Library does not exist"), |
206
654661eab7f3
More refactor, merged main.h, basics.h and utility.h into one header file basics.h and removed plenty of unused code
Teemu Piippo <teemu@hecknology.net>
parents:
201
diff
changeset
|
54 | tr("The directory %1 does not exist.").arg(quoted(dir.path())) |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
55 | ); |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
56 | } |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
57 | else |
7 | 58 | { |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
59 | if (not dir.isReadable()) |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
60 | { |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
61 | QMessageBox::warning(this, |
7 | 62 | tr("Unreadable library"), |
206
654661eab7f3
More refactor, merged main.h, basics.h and utility.h into one header file basics.h and removed plenty of unused code
Teemu Piippo <teemu@hecknology.net>
parents:
201
diff
changeset
|
63 | tr("The directory %1 cannot be read.").arg(quoted(dir.path())) |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
64 | ); |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
65 | } |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
66 | model->addLibrary({Library::OfficialLibrary, dir}); |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
67 | this->ui.newLibraryPath->clear(); |
7 | 68 | } |
69 | } | |
70 | } | |
71 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
72 | void LibrariesEditor::showContextMenu(const QPoint position) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
73 | { |
265 | 74 | const qsizetype libraryIndex = this->currentLibraryIndex(); |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
75 | LibrariesModel* model = this->currentModel(); |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
76 | if (model != nullptr and model->isValidIndex(libraryIndex)) |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
77 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
78 | QMenu* contextMenu = new QMenu{this}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
79 | QAction* removeAction = new QAction{tr("Remove library")}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
80 | connect( |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
81 | removeAction, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
82 | &QAction::triggered, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
83 | this, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
84 | &LibrariesEditor::removeCurrentLibrary); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
85 | QMenu* roleMenu = new QMenu{tr("Set role"), contextMenu}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
86 | for (const Library::Role role : Library::allRoles) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
87 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
88 | QAction* setRoleAction = new QAction{Library::libraryRoleName(role)}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
89 | setRoleAction->setData(role); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
90 | roleMenu->addAction(setRoleAction); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
91 | connect( |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
92 | setRoleAction, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
93 | &QAction::triggered, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
94 | this, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
95 | &LibrariesEditor::setCurrentLibraryRole); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
96 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
97 | QAction* moveUpAction = new QAction{tr("Move up")}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
98 | connect( |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
99 | moveUpAction, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
100 | &QAction::triggered, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
101 | this, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
102 | &LibrariesEditor::moveCurrentLibraryUp); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
103 | QAction* moveDownAction = new QAction{tr("Move down")}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
104 | connect( |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
105 | moveDownAction, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
106 | &QAction::triggered, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
107 | this, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
108 | &LibrariesEditor::moveCurrentLibraryDown); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
109 | contextMenu->addMenu(roleMenu); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
110 | contextMenu->addSeparator(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
111 | contextMenu->addAction(moveDownAction); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
112 | contextMenu->addAction(moveUpAction); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
113 | contextMenu->addAction(removeAction); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
114 | contextMenu->popup(this->ui.librariesTable->mapToGlobal(position)); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
115 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
116 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
117 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
118 | void LibrariesEditor::setCurrentLibraryRole() |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
119 | { |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
120 | if (LibrariesModel* model = this->currentModel()) { |
265 | 121 | const qsizetype libraryIndex = currentLibraryIndex(); |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
122 | QObject* senderObject = sender(); |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
123 | QAction* senderAction = qobject_cast<QAction*>(senderObject); |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
124 | const Library::Role role = senderAction->data().value<Library::Role>(); |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
125 | model->setLibraryRole(libraryIndex, role); |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
126 | } |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
127 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
128 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
129 | void LibrariesEditor::removeCurrentLibrary() |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
130 | { |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
131 | if (LibrariesModel* model = this->currentModel()) { |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
132 | model->removeLibrary(currentLibraryIndex()); |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
133 | } |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
134 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
135 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
136 | void LibrariesEditor::moveCurrentLibraryUp() |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
137 | { |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
138 | if (LibrariesModel* model = this->currentModel()) { |
265 | 139 | const qsizetype libraryIndex = this->currentLibraryIndex(); |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
140 | model->moveLibrary(libraryIndex, libraryIndex - 1); |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
141 | } |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
142 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
143 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
144 | void LibrariesEditor::moveCurrentLibraryDown() |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
145 | { |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
146 | if (LibrariesModel* model = this->currentModel()) { |
265 | 147 | const qsizetype libraryIndex = this->currentLibraryIndex(); |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
148 | model->moveLibrary(libraryIndex + 1, libraryIndex); |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
149 | } |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
150 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
151 | |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
152 | LibrariesModel *LibrariesEditor::currentModel() |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
153 | { |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
154 | return qobject_cast<LibrariesModel*>(this->ui.librariesTable->model()); |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
155 | } |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
156 | |
265 | 157 | qsizetype LibrariesEditor::currentLibraryIndex() const |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
158 | { |
265 | 159 | return this->ui.librariesTable->selectionModel()->currentIndex().row(); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
160 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
161 | |
218
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
162 | void LibrariesEditor::saveSettings() |
7 | 163 | { |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
164 | if (LibrariesModel* model = this->currentModel()) { |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
165 | model->storeToSettings(); |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
166 | } |
7 | 167 | } |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
168 | |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
169 | void LibrariesEditor::setModel(LibrariesModel *model) |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
170 | { |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
171 | this->ui.librariesTable->setModel(model); |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
172 | this->setEnabled(model != nullptr); |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
173 | } |