Browse Source

channellist: fix dual blue selection — block signals + explicit select

- selectionCommand: remove Current flag to avoid redundant
  currentChanged emission during mouse press
- loadChannels: wrap clear+repopulate in blockSignals(true/false)
  to prevent selectionChanged and currentChanged from firing
  on intermediate model states
- selectChannel: use explicit selectionModel()->select() with
  ClearAndSelect + setCurrentIndex with NoUpdate
- simplified selection restore: only track selectedCid by ID,
  removed fallback-to-row logic
topo_upd
Evgeny 2 months ago
parent
commit
3448b0a482
  1. 33
      tools/chatgui/src/channellist.cpp

33
tools/chatgui/src/channellist.cpp

@ -51,7 +51,7 @@ protected:
} }
QItemSelectionModel::SelectionFlags selectionCommand(const QModelIndex& idx, const QEvent* e) const override { QItemSelectionModel::SelectionFlags selectionCommand(const QModelIndex& idx, const QEvent* e) const override {
if (idx.isValid()) if (idx.isValid())
return QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Current; return QItemSelectionModel::ClearAndSelect;
return QItemSelectionModel::NoUpdate; return QItemSelectionModel::NoUpdate;
} }
void selectionChanged(const QItemSelection& sel, const QItemSelection& desel) override { void selectionChanged(const QItemSelection& sel, const QItemSelection& desel) override {
@ -158,17 +158,14 @@ ChannelList::ChannelList(DbManager* db, QWidget *parent)
} }
void ChannelList::loadChannels() { void ChannelList::loadChannels() {
QModelIndex curIdx = m_listView->currentIndex();
QString selectedCid; QString selectedCid;
int selectedRow = -1; if (m_listView->currentIndex().isValid())
if (curIdx.isValid()) { selectedCid = m_listView->currentIndex().data(ChannelChannelIdRole).toString();
selectedCid = curIdx.data(ChannelChannelIdRole).toString();
selectedRow = curIdx.row();
}
auto* sel = m_listView->selectionModel();
sel->blockSignals(true);
m_model->clear(); m_model->clear();
if (!m_db || !m_db->isOpen()) return; if (m_db && m_db->isOpen()) {
auto channels = m_db->getChannels(); auto channels = m_db->getChannels();
static const QColor colors[] = { static const QColor colors[] = {
QColor(74,144,226), QColor(126,211,33), QColor(245,166,35), QColor(74,144,226), QColor(126,211,33), QColor(245,166,35),
@ -189,22 +186,24 @@ void ChannelList::loadChannels() {
m_model->rowCount()-1, qPrintable(ch.channelId), qPrintable(ch.name)); m_model->rowCount()-1, qPrintable(ch.channelId), qPrintable(ch.name));
ci++; ci++;
} }
}
sel->blockSignals(false);
if (!selectedCid.isEmpty()) if (!selectedCid.isEmpty()) {
selectChannel(selectedCid); selectChannel(selectedCid);
if (!m_listView->currentIndex().isValid()) { } else if (m_model->rowCount() > 0) {
int fallbackRow = (selectedRow >= 0) ? selectedRow : 0; QModelIndex idx = m_model->index(0, 0);
if (fallbackRow >= m_model->rowCount()) sel->select(idx, QItemSelectionModel::ClearAndSelect);
fallbackRow = m_model->rowCount() - 1; sel->setCurrentIndex(idx, QItemSelectionModel::NoUpdate);
if (fallbackRow >= 0)
m_listView->setCurrentIndex(m_model->index(fallbackRow, 0));
} }
} }
void ChannelList::selectChannel(const QString& channelId) { void ChannelList::selectChannel(const QString& channelId) {
for (int row = 0; row < m_model->rowCount(); row++) { for (int row = 0; row < m_model->rowCount(); row++) {
if (m_model->item(row)->data(ChannelChannelIdRole).toString() == channelId) { if (m_model->item(row)->data(ChannelChannelIdRole).toString() == channelId) {
m_listView->setCurrentIndex(m_model->index(row, 0)); QModelIndex idx = m_model->index(row, 0);
m_listView->selectionModel()->select(idx, QItemSelectionModel::ClearAndSelect);
m_listView->selectionModel()->setCurrentIndex(idx, QItemSelectionModel::NoUpdate);
return; return;
} }
} }

Loading…
Cancel
Save