Fri, 13 Dec 2019 21:35:59 +0200
things
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 | ||
7 | LibrariesEditor::LibrariesEditor(QSettings* settings, QWidget* parent) : | |
8 | QWidget{parent}, | |
9 | libraries{settings, this}, | |
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 | { | |
51 | QMessageBox::critical(this, | |
52 | tr("Library does not exist"), | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
53 | utility::format( |
7 | 54 | tr("The directory %1 does not exist."), |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
55 | utility::quoted(dir.path()))); |
7 | 56 | } |
57 | else | |
58 | { | |
59 | if (not dir.isReadable()) | |
60 | { | |
61 | QMessageBox::warning(this, | |
62 | tr("Unreadable library"), | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
63 | utility::format( |
7 | 64 | tr("The directory %1 cannot be read."), |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
65 | utility::quoted(dir.path()))); |
7 | 66 | } |
67 | this->libraries.addLibrary({Library::OfficialLibrary, dir}); | |
68 | this->ui.newLibraryPath->clear(); | |
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 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
74 | const int libraryIndex = this->currentLibraryIndex(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
75 | if (this->libraries.isValidIndex(libraryIndex)) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
76 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
77 | QMenu* contextMenu = new QMenu{this}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
78 | QAction* removeAction = new QAction{tr("Remove library")}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
79 | connect( |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
80 | removeAction, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
81 | &QAction::triggered, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
82 | this, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
83 | &LibrariesEditor::removeCurrentLibrary); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
84 | QMenu* roleMenu = new QMenu{tr("Set role"), contextMenu}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
85 | for (const Library::Role role : Library::allRoles) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
86 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
87 | QAction* setRoleAction = new QAction{Library::libraryRoleName(role)}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
88 | setRoleAction->setData(role); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
89 | roleMenu->addAction(setRoleAction); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
90 | connect( |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
91 | setRoleAction, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
92 | &QAction::triggered, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
93 | this, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
94 | &LibrariesEditor::setCurrentLibraryRole); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
95 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
96 | QAction* moveUpAction = new QAction{tr("Move up")}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
97 | connect( |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
98 | moveUpAction, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
99 | &QAction::triggered, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
100 | this, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
101 | &LibrariesEditor::moveCurrentLibraryUp); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
102 | QAction* moveDownAction = new QAction{tr("Move down")}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
103 | connect( |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
104 | moveDownAction, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
105 | &QAction::triggered, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
106 | this, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
107 | &LibrariesEditor::moveCurrentLibraryDown); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
108 | contextMenu->addMenu(roleMenu); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
109 | contextMenu->addSeparator(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
110 | contextMenu->addAction(moveDownAction); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
111 | contextMenu->addAction(moveUpAction); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
112 | contextMenu->addAction(removeAction); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
113 | contextMenu->popup(this->ui.librariesTable->mapToGlobal(position)); |
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 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
117 | void LibrariesEditor::setCurrentLibraryRole() |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
118 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
119 | const int libraryIndex = currentLibraryIndex(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
120 | QObject* senderObject = sender(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
121 | QAction* senderAction = qobject_cast<QAction*>(senderObject); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
122 | const Library::Role role = senderAction->data().value<Library::Role>(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
123 | this->libraries.setLibraryRole(libraryIndex, role); |
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 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
126 | void LibrariesEditor::removeCurrentLibrary() |
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 | this->libraries.removeLibrary(currentLibraryIndex()); |
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 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
131 | void LibrariesEditor::moveCurrentLibraryUp() |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
132 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
133 | const int libraryIndex = this->currentLibraryIndex(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
134 | this->libraries.moveLibrary(libraryIndex, libraryIndex - 1); |
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 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
137 | void LibrariesEditor::moveCurrentLibraryDown() |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
138 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
139 | const int libraryIndex = this->currentLibraryIndex(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
140 | this->libraries.moveLibrary(libraryIndex + 1, libraryIndex); |
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 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
143 | int LibrariesEditor::currentLibraryIndex() const |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
144 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
145 | return this->ui.librariesTable->selectionModel()->currentIndex().row(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
146 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
147 | |
7 | 148 | void LibrariesEditor::saveSettings(QSettings* settings) |
149 | { | |
150 | this->libraries.storeToSettings(settings); | |
151 | } |