11/*
22 * Copyright 2016 Mitchell Krome
3- * Copyright 2017 Kai Pastor
3+ * Copyright 2017-2019 Kai Pastor
44 *
55 * This file is part of OpenOrienteering.
66 *
@@ -63,7 +63,7 @@ TagSelectWidget::TagSelectWidget(QWidget* parent)
6363 setHorizontalHeaderLabels (QStringList () << tr (" Relation" ) << tr (" Key" ) << tr (" Comparison" ) << tr (" Value" ));
6464 verticalHeader ()->setVisible (false );
6565
66- auto header_view = horizontalHeader ();
66+ auto * header_view = horizontalHeader ();
6767 header_view->setSectionsClickable (false );
6868 header_view->setSectionResizeMode (0 , QHeaderView::Fixed);
6969 header_view->setSectionResizeMode (1 , QHeaderView::Stretch);
@@ -79,6 +79,7 @@ TagSelectWidget::TagSelectWidget(QWidget* parent)
7979 addRowItems (0 );
8080
8181 connect (this , &QTableWidget::cellChanged, this , &TagSelectWidget::onCellChanged);
82+ connect (this , &QTableWidget::currentCellChanged, this , &TagSelectWidget::onCurrentCellChanged);
8283}
8384
8485
@@ -88,28 +89,31 @@ TagSelectWidget::~TagSelectWidget() = default;
8889
8990QWidget* TagSelectWidget::makeButtons (QWidget* parent)
9091{
91- auto add_button = newToolButton (QIcon (QString::fromLatin1 (" :/images/plus.png" )), tr (" Add Row" ));
92- auto delete_button = newToolButton (QIcon (QString::fromLatin1 (" :/images/minus.png" )), tr (" Remove Row" ));
92+ auto * add_button = newToolButton (QIcon (QString::fromLatin1 (" :/images/plus.png" )), tr (" Add Row" ));
93+ delete_button = newToolButton (QIcon (QString::fromLatin1 (" :/images/minus.png" )), tr (" Remove Row" ));
94+ delete_button->setEnabled (false );
9395
94- auto add_remove_layout = new SegmentedButtonLayout ();
96+ auto * add_remove_layout = new SegmentedButtonLayout ();
9597 add_remove_layout->addWidget (add_button);
9698 add_remove_layout->addWidget (delete_button);
9799
98- auto move_up_button = newToolButton (QIcon (QString::fromLatin1 (" :/images/arrow-up.png" )), tr (" Move Up" ));
100+ move_up_button = newToolButton (QIcon (QString::fromLatin1 (" :/images/arrow-up.png" )), tr (" Move Up" ));
99101 move_up_button->setAutoRepeat (true );
100- auto move_down_button = newToolButton (QIcon (QString::fromLatin1 (" :/images/arrow-down.png" )), tr (" Move Down" ));
102+ move_up_button->setEnabled (false );
103+ move_down_button = newToolButton (QIcon (QString::fromLatin1 (" :/images/arrow-down.png" )), tr (" Move Down" ));
101104 move_down_button->setAutoRepeat (true );
105+ move_down_button->setEnabled (false );
102106
103- auto up_down_layout = new SegmentedButtonLayout ();
107+ auto * up_down_layout = new SegmentedButtonLayout ();
104108 up_down_layout->addWidget (move_up_button);
105109 up_down_layout->addWidget (move_down_button);
106110
107- auto list_buttons_layout = new QHBoxLayout ();
111+ auto * list_buttons_layout = new QHBoxLayout ();
108112 list_buttons_layout->setContentsMargins (0 ,0 ,0 ,0 );
109113 list_buttons_layout->addLayout (add_remove_layout);
110114 list_buttons_layout->addLayout (up_down_layout);
111115
112- auto list_buttons_group = new QWidget (parent);
116+ auto * list_buttons_group = new QWidget (parent);
113117 list_buttons_group->setLayout (list_buttons_layout);
114118
115119 // Connections
@@ -125,7 +129,7 @@ QWidget* TagSelectWidget::makeButtons(QWidget* parent)
125129
126130QToolButton* TagSelectWidget::newToolButton (const QIcon& icon, const QString& text)
127131{
128- auto button = new QToolButton ();
132+ auto * button = new QToolButton ();
129133 button->setToolButtonStyle (Qt::ToolButtonIconOnly);
130134 button->setToolTip (text);
131135 button->setIcon (icon);
@@ -152,12 +156,12 @@ void TagSelectWidget::showEvent(QShowEvent* event)
152156
153157void TagSelectWidget::addRowItems (int row)
154158{
155- auto item = new QTableWidgetItem ();
159+ auto * item = new QTableWidgetItem ();
156160 setItem (row, 1 , item);
157161 item = new QTableWidgetItem ();
158162 setItem (row, 3 , item);
159163
160- auto compare_op = new QComboBox ();
164+ auto * compare_op = new QComboBox ();
161165 for (auto op : { ObjectQuery::OperatorIs, ObjectQuery::OperatorIsNot, ObjectQuery::OperatorContains })
162166 compare_op->addItem (ObjectQuery::labelFor (op), QVariant::fromValue (op));
163167 setCellWidget (row, 2 , compare_op);
@@ -172,7 +176,7 @@ void TagSelectWidget::addRowItems(int row)
172176 }
173177 else
174178 {
175- auto logical_op = new QComboBox ();
179+ auto * logical_op = new QComboBox ();
176180 auto and_label = QString { QLatin1String (" " ) + ObjectQuery::labelFor (ObjectQuery::OperatorAnd) };
177181 logical_op->addItem (and_label, QVariant::fromValue (ObjectQuery::OperatorAnd));
178182 logical_op->addItem (ObjectQuery::labelFor (ObjectQuery::OperatorOr), QVariant::fromValue (ObjectQuery::OperatorOr));
@@ -188,17 +192,22 @@ void TagSelectWidget::onCellChanged(int row, int column)
188192 {
189193 case 1 :
190194 case 3 :
191- {
192- auto item = this ->item (row, column);
195+ if (auto * item = this ->item (row, column))
193196 item->setText (item->text ().trimmed ());
194- break ;
195- }
197+ break ;
196198 default :
197199 ; // nothing
198200 }
199201}
200202
201203
204+ void TagSelectWidget::onCurrentCellChanged (int current_row, int /* current_column*/ , int /* previous_row*/ , int /* previous_column*/ )
205+ {
206+ move_up_button->setEnabled (current_row > 0 );
207+ move_down_button->setEnabled (current_row+1 < rowCount ());
208+ }
209+
210+
202211
203212void TagSelectWidget::addRow ()
204213{
@@ -210,6 +219,7 @@ void TagSelectWidget::addRow()
210219
211220 insertRow (row);
212221 addRowItems (row);
222+ delete_button->setEnabled (rowCount () > 1 );
213223
214224 // Move the selection to the new row
215225 int col = currentColumn ();
@@ -230,12 +240,13 @@ void TagSelectWidget::deleteRow()
230240 row = rowCount () - 1 ;
231241
232242 removeRow (row);
243+ delete_button->setEnabled (rowCount () > 1 );
233244
234245 // If we delete first row, need to fix logical operator
235246 if (row == 0 )
236247 {
237248 removeCellWidget (row, 0 );
238- auto item = new QTableWidgetItem ();
249+ auto * item = new QTableWidgetItem ();
239250 item->setFlags (Qt::NoItemFlags);
240251 item->setBackground (QBrush (QGuiApplication::palette ().window ()));
241252 setItem (row, 0 , item);
@@ -260,9 +271,13 @@ void TagSelectWidget::moveRow(bool up)
260271 if (!up)
261272 row += 1 ;
262273
274+ // Commit current cell by changing the index.
275+ auto const col = currentColumn ();
276+ setCurrentCell (row > 0 ? 0 : 1 , col);
277+
263278 // Col 1, 3 are items
264- auto top_item = takeItem (row - 1 , 1 );
265- auto bottom_item = takeItem (row, 1 );
279+ auto * top_item = takeItem (row - 1 , 1 );
280+ auto * bottom_item = takeItem (row, 1 );
266281 setItem (row, 1 , top_item);
267282 setItem (row - 1 , 1 , bottom_item);
268283 top_item = takeItem (row - 1 , 3 );
@@ -271,8 +286,8 @@ void TagSelectWidget::moveRow(bool up)
271286 setItem (row - 1 , 3 , bottom_item);
272287
273288 // Col 2 is comparison combobox
274- auto top_combo = qobject_cast<QComboBox*>(cellWidget (row - 1 , 2 ));
275- auto bottom_combo = qobject_cast<QComboBox*>(cellWidget (row, 2 ));
289+ auto * top_combo = qobject_cast<QComboBox*>(cellWidget (row - 1 , 2 ));
290+ auto * bottom_combo = qobject_cast<QComboBox*>(cellWidget (row, 2 ));
276291 auto top_selection = top_combo->currentIndex ();
277292 auto bottom_selection = bottom_combo->currentIndex ();
278293 top_combo->setCurrentIndex (bottom_selection);
@@ -290,11 +305,10 @@ void TagSelectWidget::moveRow(bool up)
290305 bottom_combo->setCurrentIndex (top_selection);
291306 }
292307
293- // Keep the moved row selected
294- int col = currentColumn ();
295308 // For the down case we already adjusted the row
296309 if (up)
297310 row -= 1 ;
311+ // Keep the moved row selected
298312 setCurrentCell (row, col);
299313}
300314
0 commit comments