From 7c6b33da1b33cf21934b3a5f2f4735ab8ee12ed8 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Wed, 3 Jul 2024 16:01:52 -0400 Subject: [PATCH] Add selection highlight to connections list --- forms/connectionslistitem.ui | 7 +++++-- forms/mainwindow.ui | 24 +++++++++++----------- include/project.h | 1 - include/ui/connectionpixmapitem.h | 4 ++++ include/ui/connectionslistitem.h | 7 +++++++ src/editor.cpp | 33 +++++++++++++++++++------------ src/mainwindow.cpp | 9 +++++---- src/project.cpp | 4 ---- src/ui/connectionpixmapitem.cpp | 27 +++++++++++++++++++------ src/ui/connectionslistitem.cpp | 18 +++++++++++++++++ 10 files changed, 92 insertions(+), 42 deletions(-) diff --git a/forms/connectionslistitem.ui b/forms/connectionslistitem.ui index a55c8d90..80e6beb5 100644 --- a/forms/connectionslistitem.ui +++ b/forms/connectionslistitem.ui @@ -6,8 +6,8 @@ 0 0 - 176 - 153 + 178 + 157 @@ -16,6 +16,9 @@ 0 + + QFrame::StyledPanel + diff --git a/forms/mainwindow.ui b/forms/mainwindow.ui index 75ceeb2e..c8bd5044 100644 --- a/forms/mainwindow.ui +++ b/forms/mainwindow.ui @@ -260,7 +260,7 @@ - 2 + 0 false @@ -769,8 +769,8 @@ 0 0 - 256 - 74 + 423 + 79 @@ -888,8 +888,8 @@ 0 0 - 256 - 74 + 423 + 79 @@ -990,10 +990,10 @@ - 0 + 8 0 - 91 - 74 + 411 + 446 @@ -1193,8 +1193,8 @@ 0 0 - 91 - 512 + 427 + 594 @@ -1413,8 +1413,8 @@ 0 0 - 93 - 460 + 382 + 699 diff --git a/include/project.h b/include/project.h index 49c0aaa8..643f74da 100644 --- a/include/project.h +++ b/include/project.h @@ -119,7 +119,6 @@ public: bool readMapGroups(); Map* addNewMapToGroup(QString, int, Map*, bool, bool); QString getNewMapName(); - bool isExistingMapName(const QString&); QString getProjectTitle(); QString readMapLayoutId(QString map_name); diff --git a/include/ui/connectionpixmapitem.h b/include/ui/connectionpixmapitem.h index c9b79226..41995003 100644 --- a/include/ui/connectionpixmapitem.h +++ b/include/ui/connectionpixmapitem.h @@ -33,6 +33,9 @@ public: bool getEditable(); void updateHighlight(bool selected); +private: + bool highlighted = false; + protected: QVariant itemChange(GraphicsItemChange change, const QVariant &value); void mousePressEvent(QGraphicsSceneMouseEvent*); @@ -42,6 +45,7 @@ signals: void connectionItemSelected(ConnectionPixmapItem* connectionItem); void connectionItemDoubleClicked(ConnectionPixmapItem* connectionItem); void connectionMoved(MapConnection*); + void highlightChanged(bool highlighted); }; #endif // CONNECTIONPIXMAPITEM_H diff --git a/include/ui/connectionslistitem.h b/include/ui/connectionslistitem.h index ffa9ddca..75f7ba5f 100644 --- a/include/ui/connectionslistitem.h +++ b/include/ui/connectionslistitem.h @@ -4,6 +4,7 @@ #include "mapconnection.h" #include +#include namespace Ui { class ConnectionsListItem; @@ -22,14 +23,20 @@ public: ~ConnectionsListItem(); void updateUI(); + void setSelected(bool selected); private: Ui::ConnectionsListItem *ui; MapConnection * const connection; + bool isSelected = false; + +protected: + void mousePressEvent(QMouseEvent *); signals: void edited(); void deleted(); + void selected(); private slots: void on_comboBox_Direction_currentTextChanged(const QString &direction); diff --git a/src/editor.cpp b/src/editor.cpp index 09922372..10249363 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -755,15 +755,29 @@ void Editor::addConnectionToList(ConnectionPixmapItem * connectionItem) { ConnectionsListItem *listItem = new ConnectionsListItem(ui->scrollAreaContents_ConnectionsList, connectionItem->connection, project->mapNames); ui->layout_ConnectionsList->insertWidget(ui->layout_ConnectionsList->count() - 1, listItem); // Insert above the vertical spacer + // Connect the pixmap item to the list item connect(connectionItem, &ConnectionPixmapItem::connectionMoved, listItem, &ConnectionsListItem::updateUI); + connect(connectionItem, &ConnectionPixmapItem::highlightChanged, listItem, &ConnectionsListItem::setSelected); + if (connectionItem == selected_connection_item) + listItem->setSelected(true); - // TODO: This is probably slower than necessary (we don't need a full redraw if we're just moving it) - // TODO: Handle mirroring + // Connect the list item to the pixmap item + connect(listItem, &ConnectionsListItem::selected, [this, connectionItem] { + if (connectionItem == selected_connection_item) { + // Already selected, no change + return; + } + // Deselect old connection and select new connection + if (selected_connection_item) selected_connection_item->updateHighlight(false); + selected_connection_item = connectionItem; + selected_connection_item->updateHighlight(true); + }); connect(listItem, &ConnectionsListItem::edited, [this, connectionItem] { + // TODO: This is probably slower than necessary (we don't need a full redraw if we're just moving it) + // TODO: Handle mirroring redrawConnection(connectionItem); emit editedMapData(); }); - connect(listItem, &ConnectionsListItem::deleted, [this, connectionItem] { removeConnection(connectionItem); }); @@ -834,16 +848,11 @@ void Editor::redrawConnection(ConnectionPixmapItem* connectionItem) { connectionItem->basePixmap = pixmap; QPoint pos = calculateConnectionPosition(connectionItem->connection, pixmap); - if (connectionItem == selected_connection_item) { - QPainter painter(&pixmap); - painter.setPen(QColor(255, 0, 255)); - painter.drawRect(0, 0, pixmap.width() - 1, pixmap.height() - 1); - painter.end(); - } + connectionItem->blockSignals(true); connectionItem->setPixmap(pixmap); + connectionItem->updateHighlight(connectionItem == selected_connection_item); connectionItem->initialX = pos.x(); connectionItem->initialY = pos.y(); - connectionItem->blockSignals(true); connectionItem->setX(pos.x()); connectionItem->setY(pos.y()); connectionItem->setZValue(-1); @@ -888,8 +897,6 @@ void Editor::onConnectionItemSelected(ConnectionPixmapItem* connectionItem) { selected_connection_item = connectionItem; for (ConnectionPixmapItem* item : connection_items) item->updateHighlight(item == selected_connection_item); - - // TODO: Handle the highlight done in redrawConnection? } // TODO: Inaccurate if there are multiple connections from the same map @@ -1851,7 +1858,7 @@ void Editor::toggleBorderVisibility(bool visible, bool enableScriptCallback) } void Editor::updateBorderVisibility() { - // On the connections tab, the border is always visible, and the connections can be edited. + // On the connections tab the border is always visible, and the connections can be edited. bool editingConnections = (ui->mainTabBar->currentIndex() == MainTab::Connections); bool visible = (editingConnections || ui->checkBox_ToggleBorder->isChecked()); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index cfea8e85..77df41fa 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -2569,26 +2569,27 @@ void MainWindow::on_pushButton_ConfigureEncountersJSON_clicked() { void MainWindow::on_button_OpenDiveMap_clicked() { const QString mapName = ui->comboBox_DiveMap->currentText(); - if (editor->project->isExistingMapName(mapName)) + if (mapName != DYNAMIC_MAP_NAME && editor->project->mapNames.contains(mapName)) userSetMap(mapName, true); } void MainWindow::on_button_OpenEmergeMap_clicked() { const QString mapName = ui->comboBox_EmergeMap->currentText(); - if (editor->project->isExistingMapName(mapName)) + if (mapName != DYNAMIC_MAP_NAME && editor->project->mapNames.contains(mapName)) userSetMap(mapName, true); } // TODO: Mirror change to/from other maps void MainWindow::on_comboBox_DiveMap_currentTextChanged(const QString &mapName) { - if (editor->project->isExistingMapName(mapName)) { + // Include empty names as an update (user is deleting the connection) + if (mapName.isEmpty() || editor->project->mapNames.contains(mapName)) { editor->updateDiveMap(mapName); markMapEdited(); } } void MainWindow::on_comboBox_EmergeMap_currentTextChanged(const QString &mapName) { - if (editor->project->isExistingMapName(mapName)) { + if (mapName.isEmpty() || editor->project->mapNames.contains(mapName)) { editor->updateEmergeMap(mapName); markMapEdited(); } diff --git a/src/project.cpp b/src/project.cpp index c1c8f5c7..c6550c6a 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -1800,10 +1800,6 @@ QString Project::getNewMapName() { return newMapName; } -bool Project::isExistingMapName(const QString &mapName) { - return !mapName.isEmpty() && mapName != DYNAMIC_MAP_NAME && this->mapNames.contains(mapName); -} - Project::DataQualifiers Project::getDataQualifiers(QString text, QString label) { Project::DataQualifiers qualifiers; diff --git a/src/ui/connectionpixmapitem.cpp b/src/ui/connectionpixmapitem.cpp index 65907490..301fcfb6 100644 --- a/src/ui/connectionpixmapitem.cpp +++ b/src/ui/connectionpixmapitem.cpp @@ -56,13 +56,23 @@ bool ConnectionPixmapItem::getEditable() { return (this->flags() & ItemIsMovable) != 0; } +// TODO: Consider whether it still makes sense to highlight the "current" connection (given you can edit them at any point now) void ConnectionPixmapItem::updateHighlight(bool selected) { - bool editable = this->getEditable(); - int zValue = (selected || !editable) ? -1 : -2; - qreal opacity = (selected || !editable) ? 1 : 0.75; - this->setZValue(zValue); - this->render(opacity); - if (editable && selected) { + const int normalZ = -1; + const qreal normalOpacity = 1.0; + + // When editing is inactive the current selection is ignored, all connections should appear normal. + if (!this->getEditable()) { + this->setZValue(normalZ); + this->render(normalOpacity); + return; + } + + this->setZValue(selected ? normalZ : -2); + this->render(selected ? normalOpacity : 0.75); + + if (selected) { + // Draw highlight QPixmap pixmap = this->pixmap(); QPainter painter(&pixmap); painter.setPen(QColor(255, 0, 255)); @@ -70,6 +80,11 @@ void ConnectionPixmapItem::updateHighlight(bool selected) { painter.end(); this->setPixmap(pixmap); } + + // Let the list UI know if the selection highlight changes so it can update accordingly + if (this->highlighted != selected) + emit highlightChanged(selected); + this->highlighted = selected; } void ConnectionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *) { diff --git a/src/ui/connectionslistitem.cpp b/src/ui/connectionslistitem.cpp index 246e83f4..096407b6 100644 --- a/src/ui/connectionslistitem.cpp +++ b/src/ui/connectionslistitem.cpp @@ -42,9 +42,25 @@ void ConnectionsListItem::updateUI() { ui->spinBox_Offset->setValue(this->connection->offset); } +// TODO: Frame shifts slightly when style changes +void ConnectionsListItem::setSelected(bool selected) { + if (selected == this->isSelected) + return; + this->isSelected = selected; + + this->setStyleSheet(selected ? ".ConnectionsListItem { border: 1px solid rgb(255, 0, 255); }" : ""); + if (selected) + emit this->selected(); +} + +void ConnectionsListItem::mousePressEvent(QMouseEvent *) { + this->setSelected(true); +} + void ConnectionsListItem::on_comboBox_Direction_currentTextChanged(const QString &direction) { this->connection->direction = direction; + this->setSelected(true); emit this->edited(); } @@ -52,6 +68,7 @@ void ConnectionsListItem::on_comboBox_Map_currentTextChanged(const QString &mapN { if (ui->comboBox_Map->findText(mapName) >= 0) { this->connection->map_name = mapName; + this->setSelected(true); emit this->edited(); } } @@ -59,6 +76,7 @@ void ConnectionsListItem::on_comboBox_Map_currentTextChanged(const QString &mapN void ConnectionsListItem::on_spinBox_Offset_valueChanged(int offset) { this->connection->offset = offset; + this->setSelected(true); emit this->edited(); }