GuiSymbols: Better handling of empty blocks

* endInsertRow was incorrectly placed before endResetModel, sometimes leading to
  bad access by the view before it was informed that the model was empty. In
  fact begin/endInsertRows were redundant with begin/endResetModel.

* Empty blocks are not supposed to be shown. This happened when moving the
  cursor. A valid block is now selected intead. Due to the design/optimisation
  of the model update, this requires a first update with an empty
  model. Simplify the old code.
This commit is contained in:
Guillaume Munch 2016-12-03 23:35:15 +01:00
parent a19be0314b
commit ee0e95aafb

View File

@ -256,10 +256,8 @@ public:
void setSymbols(QList<char_type> const & symbols, Encoding const * encoding) void setSymbols(QList<char_type> const & symbols, Encoding const * encoding)
{ {
beginResetModel(); beginResetModel();
beginInsertRows(QModelIndex(), 0, symbols.size() - 1);
symbols_ = symbols; symbols_ = symbols;
encoding_ = encoding; encoding_ = encoding;
endInsertRows();
endResetModel(); endResetModel();
} }
@ -420,8 +418,7 @@ void GuiSymbols::scrollToItem(QString const & category)
void GuiSymbols::updateSymbolList(bool update_combo) void GuiSymbols::updateSymbolList(bool update_combo)
{ {
QString category = categoryCO->currentText(); QString const category = categoryCO->currentText();
bool const nocategory = category.isEmpty();
char_type range_start = 0x0000; char_type range_start = 0x0000;
char_type range_end = 0x110000; char_type range_end = 0x110000;
QList<char_type> s; QList<char_type> s;
@ -445,11 +442,9 @@ void GuiSymbols::updateSymbolList(bool update_combo)
} }
} }
SymbolsList::const_iterator const end = symbols_.end();
int numItem = 0; int numItem = 0;
for (SymbolsList::const_iterator it = symbols_.begin(); it != end; ++it) { for (char_type c : symbols_) {
char_type c = *it; if (!update_combo && !show_all && (c < range_start || c > range_end))
if (!update_combo && !show_all && (c <= range_start || c >= range_end))
continue; continue;
QChar::Category const cat = QChar::category(uint(c)); QChar::Category const cat = QChar::category(uint(c));
// we do not want control or space characters // we do not want control or space characters
@ -460,8 +455,6 @@ void GuiSymbols::updateSymbolList(bool update_combo)
s.append(c); s.append(c);
if (update_combo) { if (update_combo) {
QString block = getBlock(c); QString block = getBlock(c);
if (category.isEmpty())
category = block;
if (used_blocks.find(block) == used_blocks.end()) if (used_blocks.find(block) == used_blocks.end())
used_blocks[block] = numItem; used_blocks[block] = numItem;
} }
@ -479,10 +472,13 @@ void GuiSymbols::updateSymbolList(bool update_combo)
int old = categoryCO->findText(category); int old = categoryCO->findText(category);
if (old != -1) if (old != -1)
categoryCO->setCurrentIndex(old); categoryCO->setCurrentIndex(old);
// update again in case the combo has not yet been filled else if (update_combo) {
// on first cycle (at dialog initialization) // restart with a non-empty block
if (nocategory && !category.isEmpty()) // this happens when the encoding changes when moving the cursor
updateSymbolList(); categoryCO->setCurrentIndex(0);
updateSymbolList(false);
return;
}
} }