Wed, 15 Jun 2022 12:32:40 +0300
GCC doesn't like me using QStringLiteral here
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> |
4 | #include "librarieseditor.h" | |
5 | #include "ui_librarieseditor.h" | |
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}, |
218
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
9 | libraries{this}, |
7 | 10 | ui{*new Ui_LibrariesEditor} |
11 | { | |
12 | this->ui.setupUi(this); | |
13 | connect( | |
14 | this->ui.newLibrarySearch, | |
15 | &QPushButton::clicked, | |
16 | this, | |
17 | &LibrariesEditor::searchPathForNewLibrary); | |
18 | connect( | |
19 | this->ui.newLibraryAdd, | |
20 | &QPushButton::clicked, | |
21 | this, | |
22 | &LibrariesEditor::addNewLibrary); | |
23 | this->ui.librariesTable->setModel(&this->libraries); | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
24 | this->ui.librariesTable->setContextMenuPolicy(Qt::CustomContextMenu); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
25 | connect( |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
26 | this->ui.librariesTable, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
27 | &QWidget::customContextMenuRequested, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
28 | this, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
29 | &LibrariesEditor::showContextMenu); |
7 | 30 | } |
31 | ||
32 | LibrariesEditor::~LibrariesEditor() | |
33 | { | |
34 | delete &this->ui; | |
35 | } | |
36 | ||
37 | void LibrariesEditor::searchPathForNewLibrary() | |
38 | { | |
39 | const QString path = QFileDialog::getExistingDirectory(this, tr("Browse LDraw library")); | |
40 | if (not path.isEmpty()) | |
41 | { | |
42 | this->ui.newLibraryPath->setText(path); | |
43 | } | |
44 | } | |
45 | ||
46 | void LibrariesEditor::addNewLibrary() | |
47 | { | |
48 | const QDir dir{this->ui.newLibraryPath->text()}; | |
49 | if (not dir.exists()) | |
50 | { | |
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
|
51 | QMessageBox::critical( |
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())) |
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
|
55 | ); |
7 | 56 | } |
57 | else | |
58 | { | |
59 | if (not dir.isReadable()) | |
60 | { | |
61 | QMessageBox::warning(this, | |
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())) |
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
|
64 | ); |
7 | 65 | } |
66 | this->libraries.addLibrary({Library::OfficialLibrary, dir}); | |
67 | this->ui.newLibraryPath->clear(); | |
68 | } | |
69 | } | |
70 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
71 | void LibrariesEditor::showContextMenu(const QPoint position) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
72 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
73 | const int libraryIndex = this->currentLibraryIndex(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
74 | if (this->libraries.isValidIndex(libraryIndex)) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
75 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
76 | QMenu* contextMenu = new QMenu{this}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
77 | QAction* removeAction = new QAction{tr("Remove library")}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
78 | connect( |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
79 | removeAction, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
80 | &QAction::triggered, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
81 | this, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
82 | &LibrariesEditor::removeCurrentLibrary); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
83 | QMenu* roleMenu = new QMenu{tr("Set role"), contextMenu}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
84 | for (const Library::Role role : Library::allRoles) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
85 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
86 | QAction* setRoleAction = new QAction{Library::libraryRoleName(role)}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
87 | setRoleAction->setData(role); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
88 | roleMenu->addAction(setRoleAction); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
89 | connect( |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
90 | setRoleAction, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
91 | &QAction::triggered, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
92 | this, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
93 | &LibrariesEditor::setCurrentLibraryRole); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
94 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
95 | QAction* moveUpAction = new QAction{tr("Move up")}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
96 | connect( |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
97 | moveUpAction, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
98 | &QAction::triggered, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
99 | this, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
100 | &LibrariesEditor::moveCurrentLibraryUp); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
101 | QAction* moveDownAction = new QAction{tr("Move down")}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
102 | connect( |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
103 | moveDownAction, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
104 | &QAction::triggered, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
105 | this, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
106 | &LibrariesEditor::moveCurrentLibraryDown); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
107 | contextMenu->addMenu(roleMenu); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
108 | contextMenu->addSeparator(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
109 | contextMenu->addAction(moveDownAction); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
110 | contextMenu->addAction(moveUpAction); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
111 | contextMenu->addAction(removeAction); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
112 | contextMenu->popup(this->ui.librariesTable->mapToGlobal(position)); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
113 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
114 | } |
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 | void LibrariesEditor::setCurrentLibraryRole() |
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 | const int libraryIndex = currentLibraryIndex(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
119 | QObject* senderObject = sender(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
120 | QAction* senderAction = qobject_cast<QAction*>(senderObject); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
121 | const Library::Role role = senderAction->data().value<Library::Role>(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
122 | this->libraries.setLibraryRole(libraryIndex, role); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
123 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
124 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
125 | void LibrariesEditor::removeCurrentLibrary() |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
126 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
127 | this->libraries.removeLibrary(currentLibraryIndex()); |
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 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
130 | void LibrariesEditor::moveCurrentLibraryUp() |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
131 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
132 | const int libraryIndex = this->currentLibraryIndex(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
133 | this->libraries.moveLibrary(libraryIndex, libraryIndex - 1); |
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::moveCurrentLibraryDown() |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
137 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
138 | const int libraryIndex = this->currentLibraryIndex(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
139 | this->libraries.moveLibrary(libraryIndex + 1, libraryIndex); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
140 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
141 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
142 | int LibrariesEditor::currentLibraryIndex() const |
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 | return this->ui.librariesTable->selectionModel()->currentIndex().row(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
145 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
146 | |
218
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
147 | void LibrariesEditor::saveSettings() |
7 | 148 | { |
218
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
149 | this->libraries.storeToSettings(); |
7 | 150 | } |