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. 71
      tools/chatgui/src/channellist.cpp

71
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,53 +158,52 @@ 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), QColor(208,2,27), QColor(144,19,254), QColor(0,184,148)
QColor(208,2,27), QColor(144,19,254), QColor(0,184,148) };
}; int ci = 0;
int ci = 0;
for (const auto& ch : channels) {
for (const auto& ch : channels) { auto letter = ch.name.mid(0, 1).toUpper();
auto letter = ch.name.mid(0, 1).toUpper(); if (letter.isEmpty()) letter = "#";
if (letter.isEmpty()) letter = "#"; auto *item = new QStandardItem();
auto *item = new QStandardItem(); item->setData(ch.name, ChannelNameRole);
item->setData(ch.name, ChannelNameRole); item->setData(ch.channelId, ChannelChannelIdRole);
item->setData(ch.channelId, ChannelChannelIdRole); item->setData(QString(), ChannelLastMessageRole);
item->setData(QString(), ChannelLastMessageRole); item->setData(makeChannelIcon(colors[ci % 6], letter), ChannelIconRole);
item->setData(makeChannelIcon(colors[ci % 6], letter), ChannelIconRole); m_model->appendRow(item);
m_model->appendRow(item); DEBUG_INFO(DEBUG_CATEGORY_DEBUG, "ChannelList::loadChannels row=%d cid='%s' name='%s'",
DEBUG_INFO(DEBUG_CATEGORY_DEBUG, "ChannelList::loadChannels row=%d cid='%s' name='%s'", 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