From 959e7ccf918ae8e67eb8d722d56068f21e3bc410 Mon Sep 17 00:00:00 2001 From: Peinthor Rene Date: Tue, 31 Jul 2012 15:55:21 +0200 Subject: [PATCH] change the order in the browse data tab on header click if a header is clicked in the browse data view the sort order of the clicked column is used/changed --- src/mainwindow.cpp | 18 +++++++++++++++++- src/mainwindow.h | 7 +++++++ src/sqlitedb.cpp | 10 ++++++---- src/sqlitedb.h | 4 ++-- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index fa1e7894..92f9312c 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -576,6 +576,7 @@ void MainWindow::setupUi() QObject::connect(helpWhatsThisAction, SIGNAL(activated()), this, SLOT(helpWhatsThis())); QObject::connect(helpAboutAction, SIGNAL(activated()), this, SLOT(helpAbout())); QObject::connect(dataTable, SIGNAL(cellDoubleClicked(int,int)), this, SLOT(doubleClickTable(int,int))); + QObject::connect(dataTable->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(browseTableHeaderClicked(int))); QObject::connect(mainTab, SIGNAL(currentChanged(int)), this, SLOT(mainTabSelected(int))); QObject::connect(executeQueryButton, SIGNAL(clicked()), this, SLOT(executeQuery())); QObject::connect(fileImportCSVAction, SIGNAL(activated()), this, SLOT(importTableFromCSV())); @@ -1022,7 +1023,8 @@ void MainWindow::populateTable( const QString & tablename, bool keepColumnWidths if (tablename.compare(db.curBrowseTableName)!=0) mustreset = true; - if (!db.browseTable(tablename)){ + QString orderby = QString::number(curBrowseOrderByIndex) + " " + (curBrowseOrderByMode == ORDERMODE_ASC ? "ASC" : "DESC"); + if (!db.browseTable(tablename, orderby)){ dataTable->setRowCount( 0 ); dataTable->setColumnCount( 0 ); QApplication::restoreOverrideCursor(); @@ -1064,6 +1066,8 @@ void MainWindow::resetBrowser() int pos = comboBrowseTable->findText(sCurrentTable); pos = pos == -1 ? 0 : pos; comboBrowseTable->setCurrentIndex(pos); + curBrowseOrderByIndex = 1; + curBrowseOrderByMode = ORDERMODE_ASC; populateTable(comboBrowseTable->currentText()); } @@ -2173,3 +2177,15 @@ void MainWindow::activateFields(bool enable) executeQueryButton->setEnabled(enable); } + +void MainWindow::browseTableHeaderClicked(int logicalindex) +{ + // instead of the column name we just use the column index, +2 because 'rowid, *' is the projection + curBrowseOrderByIndex = logicalindex + 2; + curBrowseOrderByMode = curBrowseOrderByMode == ORDERMODE_ASC ? ORDERMODE_DESC : ORDERMODE_ASC; + populateTable(comboBrowseTable->currentText(), true); + + // select the first item in the column so the header is bold + // we might try to select the last selected item + dataTable->setCurrentCell(0, logicalindex); +} diff --git a/src/mainwindow.h b/src/mainwindow.h index 60b001d2..f0a32a22 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -31,6 +31,9 @@ #include "sqlitedb.h" +#define ORDERMODE_ASC 0 +#define ORDERMODE_DESC 1 + class SQLLogDock; class editForm; class findForm; @@ -134,6 +137,9 @@ private: QAction *recentFileActs[MaxRecentFiles]; QAction *recentSeparatorAct; + int curBrowseOrderByIndex; + int curBrowseOrderByMode; + public: MainWindow(QWidget* parent = 0); ~MainWindow(); @@ -225,6 +231,7 @@ protected slots: virtual void deleteTablePopup(); virtual void editTablePopup(); virtual void mainTabSelected( int tabindex ); + virtual void browseTableHeaderClicked(int logicalindex); }; diff --git a/src/sqlitedb.cpp b/src/sqlitedb.cpp index 85805980..90a43de8 100644 --- a/src/sqlitedb.cpp +++ b/src/sqlitedb.cpp @@ -411,12 +411,12 @@ bool DBBrowserDB::updateRecord(int wrow, int wcol, const QString & wtext) } -bool DBBrowserDB::browseTable( const QString & tablename ) +bool DBBrowserDB::browseTable( const QString & tablename, const QString& orderby ) { QStringList testFields = getTableFields( tablename ); if (testFields.count()>0) {//table exists - getTableRecords( tablename ); + getTableRecords( tablename, orderby ); browseFields = testFields; hasValidBrowseSet = true; curBrowseTableName = tablename; @@ -442,7 +442,7 @@ bool DBBrowserDB::renameTable(QString from_table, QString to_table){ return true; } -void DBBrowserDB::getTableRecords( const QString & tablename ) +void DBBrowserDB::getTableRecords( const QString & tablename, const QString& orderby ) { sqlite3_stmt *vm; const char *tail; @@ -458,7 +458,9 @@ void DBBrowserDB::getTableRecords( const QString & tablename ) QString statement = "SELECT rowid, * FROM "; statement.append( GetEncodedQString(tablename) ); - statement.append(" ORDER BY rowid; "); + statement.append(" ORDER BY "); + statement.append(orderby); + statement.append(";"); //qDebug(statement); logSQL(statement, kLogMsg_App); err=sqlite3_prepare(_db,statement.toUtf8(),statement.length(), diff --git a/src/sqlitedb.h b/src/sqlitedb.h index 30ddafa1..62fa91ee 100644 --- a/src/sqlitedb.h +++ b/src/sqlitedb.h @@ -101,7 +101,7 @@ public: bool addRecord(); bool deleteRecord(int wrow); bool updateRecord(int wrow, int wcol, const QString & wtext); - bool browseTable( const QString & tablename ); + bool browseTable( const QString & tablename, const QString& orderby = "rowid" ); bool renameTable(QString from_table, QString to_table); bool createColumn(QString table, QString field, QString type); @@ -145,7 +145,7 @@ public: private: bool dirty; - void getTableRecords( const QString & tablename ); + void getTableRecords( const QString & tablename, const QString& orderby = "rowid" ); };