Skip to content

Commit ea2aa49

Browse files
mgrojojustinclift
authored andcommitted
Visual sort indicators for multi-column sorting (#1810)
This adds visual sort indicators to the already working multi-column sorting. Qt sort indicator is disabled, so only one indicator per column is visible. Unicode characters are used to indicate direction (triangles) and sort column order (superscript numbers). See issue #1761
1 parent 564d54e commit ea2aa49

File tree

4 files changed

+32
-19
lines changed

4 files changed

+32
-19
lines changed

src/FilterTableHeader.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ FilterTableHeader::FilterTableHeader(QTableView* parent) :
1010
{
1111
// Activate the click signals to allow sorting
1212
setSectionsClickable(true);
13-
setSortIndicatorShown(true);
13+
// But use our own indicators allowing multi-column sorting
14+
setSortIndicatorShown(false);
1415

1516
// Do some connects: Basically just resize and reposition the input widgets whenever anything changes
1617
connect(this, SIGNAL(sectionResized(int,int,int)), this, SLOT(adjustPositions()));

src/MainWindow.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -749,9 +749,6 @@ void MainWindow::populateTable()
749749
for(int i=1;i<m_browseTableModel->columnCount();i++)
750750
ui->dataTable->setColumnWidth(i, ui->dataTable->horizontalHeader()->defaultSectionSize());
751751

752-
// Sorting
753-
ui->dataTable->filterHeader()->setSortIndicator(0, Qt::AscendingOrder);
754-
755752
// Encoding
756753
m_browseTableModel->setEncoding(defaultBrowseTableEncoding);
757754

@@ -849,15 +846,6 @@ void MainWindow::applyBrowseTableSettings(BrowseDataTableSettings storedData, bo
849846
for(auto widthIt=storedData.columnWidths.constBegin();widthIt!=storedData.columnWidths.constEnd();++widthIt)
850847
ui->dataTable->setColumnWidth(widthIt.key(), widthIt.value());
851848

852-
// Sorting
853-
// For now just use the first sort column for the sort indicator
854-
if(storedData.query.orderBy().size())
855-
{
856-
ui->dataTable->filterHeader()->setSortIndicator(
857-
storedData.query.orderBy().front().column,
858-
storedData.query.orderBy().front().direction == sqlb::Ascending ? Qt::AscendingOrder : Qt::DescendingOrder);
859-
}
860-
861849
// Filters
862850
if(!skipFilters)
863851
{

src/MainWindow.ui

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,6 @@ You can drag SQL statements from an object row and drop them into other applicat
222222
<property name="selectionMode">
223223
<enum>QAbstractItemView::ContiguousSelection</enum>
224224
</property>
225-
<attribute name="horizontalHeaderShowSortIndicator" stdset="0">
226-
<bool>true</bool>
227-
</attribute>
228225
</widget>
229226
</item>
230227
<item>

src/sqlitetablemodel.cpp

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,23 @@ int SqliteTableModel::filterCount() const
217217
return m_query.where().size();
218218
}
219219

220+
// Convert a number to string using the Unicode superscript characters
221+
static QString toSuperScript(int number)
222+
{
223+
QString superScript = QString::number(number);
224+
superScript.replace("0", "");
225+
superScript.replace("1", "¹");
226+
superScript.replace("2", "²");
227+
superScript.replace("3", "³");
228+
superScript.replace("4", "");
229+
superScript.replace("5", "");
230+
superScript.replace("6", "");
231+
superScript.replace("7", "");
232+
superScript.replace("8", "");
233+
superScript.replace("9", "");
234+
return superScript;
235+
}
236+
220237
QVariant SqliteTableModel::headerData(int section, Qt::Orientation orientation, int role) const
221238
{
222239
if (role != Qt::DisplayRole)
@@ -225,9 +242,19 @@ QVariant SqliteTableModel::headerData(int section, Qt::Orientation orientation,
225242
if (orientation == Qt::Horizontal)
226243
{
227244
// if we have a VIRTUAL table the model will not be valid, with no header data
228-
if(section < m_headers.size())
229-
return m_headers.at(section);
230-
245+
if(section < m_headers.size()) {
246+
QString sortIndicator;
247+
for(int i = 0; i < m_query.orderBy().size(); i++) {
248+
const sqlb::SortedColumn sortedColumn = m_query.orderBy()[i];
249+
// Append sort indicator with direction and ordinal number in superscript style
250+
if (sortedColumn.column == section) {
251+
sortIndicator = sortedColumn.direction == sqlb::Ascending ? "" : "";
252+
sortIndicator.append(toSuperScript(i+1));
253+
break;
254+
}
255+
}
256+
return m_headers.at(section) + sortIndicator;
257+
}
231258
return QString("%1").arg(section + 1);
232259
}
233260
else

0 commit comments

Comments
 (0)