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
This commit is contained in:
Peinthor Rene
2012-07-31 15:55:21 +02:00
parent d45678c551
commit 959e7ccf91
4 changed files with 32 additions and 7 deletions

View File

@@ -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);
}

View File

@@ -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);
};

View File

@@ -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(),

View File

@@ -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" );
};