Skip to content

Commit

Permalink
Implemented multiple selection for roaming zones. Also fixed add, rem…
Browse files Browse the repository at this point in the history
… and move of roaming zones. Fixes #76.
  • Loading branch information
hmatuschek committed Mar 30, 2021
1 parent 3a9e78e commit afba9c4
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 25 deletions.
24 changes: 24 additions & 0 deletions lib/roaming.cc
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,18 @@ RoamingZoneList::moveUp(int row) {
return true;
}

bool
RoamingZoneList::moveUp(int first, int last) {
if ((0>=first) || (last>=count()))
return false;
beginMoveRows(QModelIndex(), first, last, QModelIndex(), first-1);
for (int row=first; row<=last; row++)
std::swap(_zones[row], _zones[row-1]);
endMoveRows();
emit modified();
return true;
}

bool
RoamingZoneList::moveDown(int row) {
if ((0>row) || ((row-1)>=count()))
Expand All @@ -238,6 +250,18 @@ RoamingZoneList::moveDown(int row) {
return true;
}

bool
RoamingZoneList::moveDown(int first, int last) {
if ((0>first) || ((last+1)>=count()))
return false;
beginMoveRows(QModelIndex(), first, last, QModelIndex(), last+2);
for (int row=last; row>=first; row--)
std::swap(_zones[row], _zones[row+1]);
endMoveRows();
emit modified();
return true;
}

int
RoamingZoneList::rowCount(const QModelIndex &idx) const {
Q_UNUSED(idx);
Expand Down
4 changes: 4 additions & 0 deletions lib/roaming.hh
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,12 @@ public:

/** Moves the roaming zone at the given row one up. */
bool moveUp(int row);
/** Moves the roaming zones one up. */
bool moveUp(int first, int last);
/** Moves the roaming zone at the given row one down. */
bool moveDown(int row);
/** Moves the roaming zones one down. */
bool moveDown(int first, int last);

/** Implementation of QAbstractListModel, returns the number of rows. */
int rowCount(const QModelIndex &parent = QModelIndex()) const;
Expand Down
3 changes: 3 additions & 0 deletions shared/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,9 @@
<layout class="QHBoxLayout" name="horizontalLayout_13">
<item>
<widget class="QListView" name="roamingZoneList">
<property name="selectionMode">
<enum>QAbstractItemView::ContiguousSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
Expand Down
81 changes: 56 additions & 25 deletions src/application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1466,13 +1466,10 @@ Application::onAddRoamingZone() {
return;

QListView *list = _mainWindow->findChild<QListView *>("roamingZoneList");
QModelIndex selected = list->selectionModel()->currentIndex();

RoamingZone *zone = dialog.zone();
if (selected.isValid())
_config->roaming()->addZone(zone, selected.row()+1);
else
_config->roaming()->addZone(zone);
int row=-1;
if (list->selectionModel()->hasSelection())
row = list->selectionModel()->selection().back().bottom()+1;
_config->roaming()->addZone(dialog.zone(), row);
}

void
Expand All @@ -1497,46 +1494,80 @@ Application::onGenRoamingZone() {
zone->deleteLater();
return;
}
dialog.zone();
_config->roaming()->addZone(zone);

QListView *list = _mainWindow->findChild<QListView *>("roamingZoneList");
int row=-1;
if (list->selectionModel()->hasSelection())
row = list->selectionModel()->selection().back().bottom()+1;
_config->roaming()->addZone(dialog.zone(), row);
}

void
Application::onRemRoamingZone() {
QModelIndex idx = _mainWindow->findChild<QListView *>("roamingZoneList")->selectionModel()->currentIndex();
if (! idx.isValid()) {
QListView *list = _mainWindow->findChild<QListView *>("roamingZoneList");
if (! list->selectionModel()->hasSelection()) {
QMessageBox::information(
nullptr, tr("Cannot delete roaming zone"),
tr("Cannot delete roaming zone: You have to select a zone first."));
return;
}

QString name = _config->roaming()->zone(idx.row())->name();
if (QMessageBox::No == QMessageBox::question(
nullptr, tr("Delete roaming zone?"), tr("Delete roaming zone %1?").arg(name)))
return;

_config->roaming()->remZone(idx.row());
// Get selection and ask for deletion
QList<int> rows = getSelectionRows(list->selectionModel()->selection().indexes());
if (1 == rows.count()) {
QString name = _config->roaming()->zone(rows.first())->name();
if (QMessageBox::No == QMessageBox::question(
nullptr, tr("Delete roaming zone?"), tr("Delete roaming zone %1?").arg(name)))
return;
} else {
if (QMessageBox::No == QMessageBox::question(
nullptr, tr("Delete roaming zones?"), tr("Delete %1 roaming zones?").arg(rows.count())))
return;
}
// collect all selected zones
// need to collect them first as rows change when deleting
QList<RoamingZone *> lists; lists.reserve(rows.count());
foreach (int row, rows)
lists.push_back(_config->roaming()->zone(row));
// remove
foreach (RoamingZone *zone, lists)
_config->roaming()->remZone(zone);
}

void
Application::onRoamingZoneUp() {
QListView *list = _mainWindow->findChild<QListView *>("roamingZoneList");
QModelIndex selected = list->selectionModel()->currentIndex();
if ((! selected.isValid()) || (0 >= selected.row()))
// Check if there is a selection
if (! list->selectionModel()->hasSelection()) {
QMessageBox::information(
nullptr, tr("Cannot move roaming zones."),
tr("Cannot move roaming zones: You have to select at least one roaming zone first."));
return;
if (_config->roaming()->moveUp(selected.row()))
list->setCurrentIndex(_config->roaming()->index(selected.row()-1));
}
// Get selection range assuming only continious selection mode
QPair<int, int> rows = getSelectionRowRange(list->selectionModel()->selection().indexes());
if ((0>rows.first) || (0>rows.second))
return;
// Then move rows
_config->roaming()->moveUp(rows.first, rows.second);
}

void
Application::onRoamingZoneDown() {
QListView *list = _mainWindow->findChild<QListView *>("roamingZoneList");
QModelIndex selected = list->selectionModel()->currentIndex();
if ((! selected.isValid()) || ((_config->roaming()->count()-1) <= selected.row()))
// Check if there is a selection
if (! list->selectionModel()->hasSelection()) {
QMessageBox::information(
nullptr, tr("Cannot move roaming zones."),
tr("Cannot move roaming zones: You have to select at least one roaming zone first."));
return;
if (_config->roaming()->moveDown(selected.row()))
list->setCurrentIndex(_config->roaming()->index(selected.row()+1));
}
// Get selection range assuming only continious selection mode
QPair<int, int> rows = getSelectionRowRange(list->selectionModel()->selection().indexes());
if ((0>rows.first) || (0>rows.second))
return;
// Then move rows
_config->roaming()->moveDown(rows.first, rows.second);
}

void
Expand Down

0 comments on commit afba9c4

Please sign in to comment.