src/settingseditor/librarieseditor.cpp

changeset 8
44679e468ba9
parent 7
68443f5be176
child 41
0abada2a9802
equal deleted inserted replaced
7:68443f5be176 8:44679e468ba9
1 #include <QFileDialog> 1 #include <QFileDialog>
2 #include <QMenu>
2 #include <QMessageBox> 3 #include <QMessageBox>
3 #include "librarieseditor.h" 4 #include "librarieseditor.h"
4 #include "ui_librarieseditor.h" 5 #include "ui_librarieseditor.h"
5 6
6 LibrariesEditor::LibrariesEditor(QSettings* settings, QWidget* parent) : 7 LibrariesEditor::LibrariesEditor(QSettings* settings, QWidget* parent) :
18 this->ui.newLibraryAdd, 19 this->ui.newLibraryAdd,
19 &QPushButton::clicked, 20 &QPushButton::clicked,
20 this, 21 this,
21 &LibrariesEditor::addNewLibrary); 22 &LibrariesEditor::addNewLibrary);
22 this->ui.librariesTable->setModel(&this->libraries); 23 this->ui.librariesTable->setModel(&this->libraries);
24 this->ui.librariesTable->setContextMenuPolicy(Qt::CustomContextMenu);
25 connect(
26 this->ui.librariesTable,
27 &QWidget::customContextMenuRequested,
28 this,
29 &LibrariesEditor::showContextMenu);
23 } 30 }
24 31
25 LibrariesEditor::~LibrariesEditor() 32 LibrariesEditor::~LibrariesEditor()
26 { 33 {
27 delete &this->ui; 34 delete &this->ui;
41 const QDir dir{this->ui.newLibraryPath->text()}; 48 const QDir dir{this->ui.newLibraryPath->text()};
42 if (not dir.exists()) 49 if (not dir.exists())
43 { 50 {
44 QMessageBox::critical(this, 51 QMessageBox::critical(this,
45 tr("Library does not exist"), 52 tr("Library does not exist"),
46 format( 53 utility::format(
47 tr("The directory %1 does not exist."), 54 tr("The directory %1 does not exist."),
48 quoted(dir.path()))); 55 utility::quoted(dir.path())));
49 } 56 }
50 else 57 else
51 { 58 {
52 if (not dir.isReadable()) 59 if (not dir.isReadable())
53 { 60 {
54 QMessageBox::warning(this, 61 QMessageBox::warning(this,
55 tr("Unreadable library"), 62 tr("Unreadable library"),
56 format( 63 utility::format(
57 tr("The directory %1 cannot be read."), 64 tr("The directory %1 cannot be read."),
58 quoted(dir.path()))); 65 utility::quoted(dir.path())));
59 } 66 }
60 this->libraries.addLibrary({Library::OfficialLibrary, dir}); 67 this->libraries.addLibrary({Library::OfficialLibrary, dir});
61 this->ui.newLibraryPath->clear(); 68 this->ui.newLibraryPath->clear();
62 } 69 }
63 } 70 }
64 71
72 void LibrariesEditor::showContextMenu(const QPoint position)
73 {
74 const int libraryIndex = this->currentLibraryIndex();
75 if (this->libraries.isValidIndex(libraryIndex))
76 {
77 QMenu* contextMenu = new QMenu{this};
78 QAction* removeAction = new QAction{tr("Remove library")};
79 connect(
80 removeAction,
81 &QAction::triggered,
82 this,
83 &LibrariesEditor::removeCurrentLibrary);
84 QMenu* roleMenu = new QMenu{tr("Set role"), contextMenu};
85 for (const Library::Role role : Library::allRoles)
86 {
87 QAction* setRoleAction = new QAction{Library::libraryRoleName(role)};
88 setRoleAction->setData(role);
89 roleMenu->addAction(setRoleAction);
90 connect(
91 setRoleAction,
92 &QAction::triggered,
93 this,
94 &LibrariesEditor::setCurrentLibraryRole);
95 }
96 QAction* moveUpAction = new QAction{tr("Move up")};
97 connect(
98 moveUpAction,
99 &QAction::triggered,
100 this,
101 &LibrariesEditor::moveCurrentLibraryUp);
102 QAction* moveDownAction = new QAction{tr("Move down")};
103 connect(
104 moveDownAction,
105 &QAction::triggered,
106 this,
107 &LibrariesEditor::moveCurrentLibraryDown);
108 contextMenu->addMenu(roleMenu);
109 contextMenu->addSeparator();
110 contextMenu->addAction(moveDownAction);
111 contextMenu->addAction(moveUpAction);
112 contextMenu->addAction(removeAction);
113 contextMenu->popup(this->ui.librariesTable->mapToGlobal(position));
114 }
115 }
116
117 void LibrariesEditor::setCurrentLibraryRole()
118 {
119 const int libraryIndex = currentLibraryIndex();
120 QObject* senderObject = sender();
121 QAction* senderAction = qobject_cast<QAction*>(senderObject);
122 const Library::Role role = senderAction->data().value<Library::Role>();
123 this->libraries.setLibraryRole(libraryIndex, role);
124 }
125
126 void LibrariesEditor::removeCurrentLibrary()
127 {
128 this->libraries.removeLibrary(currentLibraryIndex());
129 }
130
131 void LibrariesEditor::moveCurrentLibraryUp()
132 {
133 const int libraryIndex = this->currentLibraryIndex();
134 this->libraries.moveLibrary(libraryIndex, libraryIndex - 1);
135 }
136
137 void LibrariesEditor::moveCurrentLibraryDown()
138 {
139 const int libraryIndex = this->currentLibraryIndex();
140 this->libraries.moveLibrary(libraryIndex + 1, libraryIndex);
141 }
142
143 int LibrariesEditor::currentLibraryIndex() const
144 {
145 return this->ui.librariesTable->selectionModel()->currentIndex().row();
146 }
147
65 void LibrariesEditor::saveSettings(QSettings* settings) 148 void LibrariesEditor::saveSettings(QSettings* settings)
66 { 149 {
67 this->libraries.storeToSettings(settings); 150 this->libraries.storeToSettings(settings);
68 } 151 }

mercurial