Debian Codegraph @

src/CommandDialog.cpp

raw symbols SCIP
1 #include "ScopedAssign.h"
2 #include "CommandDialog.h"
3 #include <QStandardItemModel>
4
5
6 enum CommandColumns {
7 ICON = 0, NAME, DESCRIPTION, SOURCE, MAX
8 };
9
10
11 CommandDialog::CommandDialog(QList<CommandRef>& commands, QWidget* parent)
12 : QDialog(parent), commands(commands)
13 {
14 setupUi(this);
15
16 connect(btnAdd, &QPushButton::clicked, this, &CommandDialog::onAddButtonClicked);
17 connect(btnRemove, &QPushButton::clicked, this, &CommandDialog::onRemoveButtonClicked);
18
19 connect(okButton, &QPushButton::clicked, this, &CommandDialog::accept);
20 connect(cancelButton, &QPushButton::clicked, this, &CommandDialog::reject);
21
22 refresh();
23 }
24
25 void CommandDialog::refresh()
26 {
27 for (const auto& command : commands) {
28 createItem();
29 writeCommand(command);
30 }
31 twCommands->resizeColumnsToContents();
32 }
33
34 void CommandDialog::onAddButtonClicked()
35 {
36 auto sa = ScopedAssign(userMode, false);
37 createCommand();
38 twCommands->resizeColumnsToContents();
39 }
40
41 void CommandDialog::onRemoveButtonClicked()
42 {
43 if (twCommands->currentRow() == -1) return;
44
45 auto sa = ScopedAssign(userMode, false);
46 twCommands->removeRow(twCommands->currentRow());
47 }
48
49 int CommandDialog::createItem()
50 {
51 int row = twCommands->rowCount();
52 twCommands->setRowCount(row + 1);
53 twCommands->selectRow(row);
54
55 for (auto column : {ICON, NAME, DESCRIPTION, SOURCE}) {
56 auto* item = new QTableWidgetItem();
57 item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable);
58 twCommands->setItem(row, column, item);
59 }
60
61 return row;
62 }
63
64 void CommandDialog::createCommand()
65 {
66 // get next available command name
67 QString cmdName = {};
68 bool found = false;
69 do {
70 cmdName = QString(tr("Command_%1")).arg(counter++);
71 found = false;
72 for (auto& command : commands) {
73 if (command.name == cmdName) {
74 found = true;
75 break;
76 }
77 }
78 } while (found);
79
80 int index = createItem();
81 writeCommand({cmdName, {}, {}, ":/icons/gear.png", index});
82 }
83
84 void CommandDialog::writeCommand(const CommandRef& command)
85 {
86 auto* item0 = twCommands->item(command.index, ICON);
87 item0->setText(command.icon);
88
89 auto* item1 = twCommands->item(command.index, NAME);
90 item1->setText(command.name);
91
92 auto* item2 = twCommands->item(command.index, DESCRIPTION);
93 item2->setText(command.description);
94
95 auto* item3 = twCommands->item(command.index, SOURCE);
96 item3->setText(command.source);
97 }
98
99 void CommandDialog::accept()
100 {
101 commands.clear();
102 for (int row = 0; row < twCommands->rowCount(); ++row) {
103 auto icon = twCommands->item(row, ICON)->text();
104 auto name = twCommands->item(row, NAME)->text();
105 auto description = twCommands->item(row, DESCRIPTION)->text();
106 auto source = twCommands->item(row, SOURCE)->text();
107 const CommandRef command{name, description, source, icon, row};
108 commands.append(command);
109 }
110 done(QDialog::Accepted);
111 }
112
113 void CommandDialog::reject()
114 {
115 done(QDialog::Rejected);
116 }