comparison widgets/PluginPathConfigurator.cpp @ 1287:2dd7f764c3a6 plugin-path-config

Better editing mechanism
author Chris Cannam
date Tue, 15 May 2018 13:56:26 +0100
parents e327bbf4bf57
children 4683b6ffb76a
comparison
equal deleted inserted replaced
1286:e327bbf4bf57 1287:2dd7f764c3a6
13 */ 13 */
14 14
15 #include "PluginPathConfigurator.h" 15 #include "PluginPathConfigurator.h"
16 16
17 #include <QPushButton> 17 #include <QPushButton>
18 #include <QListWidget>
18 #include <QLabel> 19 #include <QLabel>
19 20
20 #include "IconLoader.h" 21 #include "IconLoader.h"
21 #include "WidgetScale.h" 22 #include "WidgetScale.h"
22 23
23 PluginPathConfigurator::PluginPathConfigurator(QWidget *parent) : 24 PluginPathConfigurator::PluginPathConfigurator(QWidget *parent) :
24 QFrame(parent), 25 QFrame(parent)
25 m_innerFrame(0)
26 { 26 {
27 setFrameStyle(StyledPanel | Sunken);
28 m_layout = new QGridLayout; 27 m_layout = new QGridLayout;
29 setLayout(m_layout); 28 setLayout(m_layout);
29
30 int row = 0;
31
32 QLabel *header = new QLabel;
33 header->setText(tr("Plugin locations"));
34 m_layout->addWidget(header, row, 0);
35 ++row;
36
37 m_list = new QListWidget;
38 m_layout->addWidget(m_list, row, 0, 1, 2);
39 m_layout->setRowStretch(row, 10);
40 m_layout->setColumnStretch(0, 10);
41 ++row;
42
43 QHBoxLayout *buttons = new QHBoxLayout;
44
45 QPushButton *down = new QPushButton;
46 down->setIcon(IconLoader().load("down"));
47 down->setToolTip(tr("Move the selected location down in the list"));
48 down->setFixedSize(WidgetScale::scaleQSize(QSize(16, 16)));
49 connect(down, SIGNAL(clicked()), this, SLOT(downClicked()));
50 buttons->addWidget(down);
51
52 QPushButton *up = new QPushButton;
53 up->setIcon(IconLoader().load("up"));
54 up->setToolTip(tr("Move the selected location up in the list"));
55 up->setFixedSize(WidgetScale::scaleQSize(QSize(16, 16)));
56 connect(up, SIGNAL(clicked()), this, SLOT(upClicked()));
57 buttons->addWidget(up);
58
59 QPushButton *del = new QPushButton;
60 del->setIcon(IconLoader().load("datadelete"));
61 del->setToolTip(tr("Remove the selected location from the list"));
62 del->setFixedSize(WidgetScale::scaleQSize(QSize(16, 16)));
63 connect(del, SIGNAL(clicked()), this, SLOT(deleteClicked()));
64 buttons->addWidget(del);
65
66 m_layout->addLayout(buttons, row, 1);
67 ++row;
30 } 68 }
31 69
32 PluginPathConfigurator::~PluginPathConfigurator() 70 PluginPathConfigurator::~PluginPathConfigurator()
33 { 71 {
34 } 72 }
40 m_var = envVariable; 78 m_var = envVariable;
41 populate(); 79 populate();
42 } 80 }
43 81
44 void 82 void
45 PluginPathConfigurator::populate() 83 PluginPathConfigurator::populate(int makeCurrent)
46 { 84 {
47 delete m_innerFrame; 85 m_list->clear();
48 m_innerFrame = new QWidget;
49 m_layout->addWidget(m_innerFrame, 0, 0);
50 86
51 QGridLayout *innerLayout = new QGridLayout;
52 m_innerFrame->setLayout(innerLayout);
53
54 QLabel *header = new QLabel(m_innerFrame);
55 header->setText(QString("<b>%1</b>").arg(tr("Location")));
56 innerLayout->addWidget(header, 0, 3);
57
58 for (int i = 0; i < m_path.size(); ++i) { 87 for (int i = 0; i < m_path.size(); ++i) {
88 m_list->addItem(m_path[i]);
89 }
59 90
60 int col = 0; 91 if (makeCurrent >= 0 && makeCurrent < m_path.size()) {
61 int row = i + 1; 92 m_list->setCurrentRow(makeCurrent);
62 QString dir = m_path[i];
63
64 if (i > 0) {
65 QPushButton *up = new QPushButton(m_innerFrame);
66 up->setObjectName(QString("%1").arg(i));
67 up->setIcon(IconLoader().load("up"));
68 up->setFixedSize(WidgetScale::scaleQSize(QSize(16, 16)));
69 connect(up, SIGNAL(clicked()), this, SLOT(upClicked()));
70 innerLayout->addWidget(up, row, col);
71 }
72 ++col;
73
74 if (i + 1 < m_path.size()) {
75 QPushButton *down = new QPushButton(m_innerFrame);
76 down->setObjectName(QString("%1").arg(i));
77 down->setIcon(IconLoader().load("down"));
78 down->setFixedSize(WidgetScale::scaleQSize(QSize(16, 16)));
79 connect(down, SIGNAL(clicked()), this, SLOT(downClicked()));
80 innerLayout->addWidget(down, row, col);
81 }
82 ++col;
83
84 QPushButton *del = new QPushButton(m_innerFrame);
85 del->setObjectName(QString("%1").arg(i));
86 del->setIcon(IconLoader().load("datadelete"));
87 del->setFixedSize(WidgetScale::scaleQSize(QSize(16, 16)));
88 connect(del, SIGNAL(clicked()), this, SLOT(deleteClicked()));
89 innerLayout->addWidget(del, row, col);
90 ++col;
91
92 QLabel *dirLabel = new QLabel(m_innerFrame);
93 dirLabel->setObjectName(QString("%1").arg(i));
94 dirLabel->setText(dir);
95 innerLayout->addWidget(dirLabel, row, col);
96 innerLayout->setColumnStretch(col, 10);
97 ++col;
98 } 93 }
99 } 94 }
100 95
101 void 96 void
102 PluginPathConfigurator::upClicked() 97 PluginPathConfigurator::upClicked()
103 { 98 {
104 bool ok = false; 99 int current = m_list->currentRow();
105 int n = sender()->objectName().toInt(&ok); 100 if (current <= 0) return;
106 if (!ok) { 101
107 SVCERR << "upClicked: unable to find index" << endl;
108 return;
109 }
110 QStringList newPath; 102 QStringList newPath;
111 for (int i = 0; i < m_path.size(); ++i) { 103 for (int i = 0; i < m_path.size(); ++i) {
112 if (i + 1 == n) { 104 if (i + 1 == current) {
113 newPath.push_back(m_path[i+1]); 105 newPath.push_back(m_path[i+1]);
114 newPath.push_back(m_path[i]); 106 newPath.push_back(m_path[i]);
115 ++i; 107 ++i;
116 } else { 108 } else {
117 newPath.push_back(m_path[i]); 109 newPath.push_back(m_path[i]);
118 } 110 }
119 } 111 }
120 m_path = newPath; 112 m_path = newPath;
121 populate(); 113
114 populate(current - 1);
122 } 115 }
123 116
124 void 117 void
125 PluginPathConfigurator::downClicked() 118 PluginPathConfigurator::downClicked()
126 { 119 {
127 bool ok = false; 120 int current = m_list->currentRow();
128 int n = sender()->objectName().toInt(&ok); 121 if (current < 0 || current + 1 >= m_path.size()) return;
129 if (!ok) { 122
130 SVCERR << "downClicked: unable to find index" << endl;
131 return;
132 }
133 QStringList newPath; 123 QStringList newPath;
134 for (int i = 0; i < m_path.size(); ++i) { 124 for (int i = 0; i < m_path.size(); ++i) {
135 if (i == n) { 125 if (i == current) {
136 newPath.push_back(m_path[i+1]); 126 newPath.push_back(m_path[i+1]);
137 newPath.push_back(m_path[i]); 127 newPath.push_back(m_path[i]);
138 ++i; 128 ++i;
139 } else { 129 } else {
140 newPath.push_back(m_path[i]); 130 newPath.push_back(m_path[i]);
141 } 131 }
142 } 132 }
143 m_path = newPath; 133 m_path = newPath;
144 populate(); 134
135 populate(current + 1);
145 } 136 }
146 137
147 void 138 void
148 PluginPathConfigurator::deleteClicked() 139 PluginPathConfigurator::deleteClicked()
149 { 140 {
150 bool ok = false; 141 int current = m_list->currentRow();
151 int n = sender()->objectName().toInt(&ok); 142
152 if (!ok) {
153 SVCERR << "deleteClicked: unable to find index" << endl;
154 return;
155 }
156 QStringList newPath; 143 QStringList newPath;
157 for (int i = 0; i < m_path.size(); ++i) { 144 for (int i = 0; i < m_path.size(); ++i) {
158 if (i != n) { 145 if (i != current) {
159 newPath.push_back(m_path[i]); 146 newPath.push_back(m_path[i]);
160 } 147 }
161 } 148 }
162 m_path = newPath; 149 m_path = newPath;
163 populate(); 150
151 populate(current < m_path.size() ? current : current-1);
164 } 152 }