Remove the hard-coded number of columns and get the number of selected
packages right.
Details
- Reviewers
sitter - Commits
- R550:7a1826f19558: Get the right number of selected packages
Diff Detail
- Repository
- R550 Muon
- Lint
Automatic diff as part of commit; lint not applicable. - Unit
Automatic diff as part of commit; unit tests not applicable.
src/PackageModel/PackageView.cpp | ||
---|---|---|
56 | (count <= 0) | |
src/PackageModel/PackageWidget.cpp | ||
112 | Given this 3 is still hardcoded you'll want to Q_ASSERT(numColumns >= 3) before the loop. For the record: I think the actual way to write this smartly is to have PackageModel::headerData implement a UserRole that returns an enum value. The enum would be the header types. Then you iter all columns and hide the headers based on their enum value. Which means nothing needs hardcoding and should more columns get added later the switch cases handling would light up in compiler warnings so no one can forget to handle the new headers to either hide or show by default. | |
316 | (selected <= 0) |
src/PackageModel/PackageWidget.cpp | ||
---|---|---|
112 | Thanks for the hint, recorded as bug 391465. The PackageModel is strange anyway, in headerData() it returns labels for each section for the DisplayRole. But in data() it does not care about the column. Instead icon, description, status, etc. are implemented as roles. |
The custom roles in data() ignoring the column isn't necessarily a problem, but combined with the callers not knowing what is in a column it gets problematic. e.g. if you look at the Delegate's paintText, it wants to do different rendering based on the "meaning" of the column but ultimately needs to hardcode the columns because there is no way to determine it programmatically.
If you made data() fully index and role aware you could turn the Delegate from
switch (index.column()) { case 1: state = index.data(PackageModel::StatusRole).toInt(); if (state & QApt::Package::NowBroken) { text = MuonStrings::global()->packageStateName(QApt::Package::NowBroken); pen.setBrush(color.foreground(KColorScheme::NegativeText)); break; }
into this, by basically moving all the switch casing into the model's data():
text = index.data(Qt::DisplayRole).toInt(); // Internally: MuonStrings::global()->packageStateName(m_package.at(i)->state()); pen.setBrush(index.data(Qt::ForegroundRole)); // Internally: switch(m_package.at(i)->state()) { ... return color.foreground(KColorScheme::NegativeText) }
BUT
this ultimately moves most of the theming decision going on into the model. All the code would still be there, just living in the Model somewhere.
The current code takes a different approach in that it makes the theming decision in the delegate (which IMO is better UI/logic separation).
Either approach is fine and basically the same amount of code (albeit in different files). The current code's main deficiency is with assigning meaning to columns though. i.e. a proper implementation of delegate-side theming would have to be something like:
NewFancyColumRoleEnum role = index.model().headerData(index.column(), Qt::Vertical, NewFancyColumRoleEnum); switch (role) { case StateColumnRole: state = index.data(PackageModel::StatusRole).toInt(); if (state & QApt::Package::NowBroken) { text = MuonStrings::global()->packageStateName(QApt::Package::NowBroken); pen.setBrush(color.foreground(KColorScheme::NegativeText)); break; }
Anyway, food for thought.