Support alternative display formats in the Browse Data tab

This is a proof-of-concept or even a basic first implementation of a new
feature I'd like to have in DB4S which at the moment I call display
formats.

The idea here is to allow the user to change the data in the Browse Data
tab on a per column basis before displaying it. This means even though
the data is stored in format X in the database it can be shown in format
Y in the browser. This should be useful in cases where the original
format X is hard to read or just not useful in a particular case.

This first implementation allows the user to right click on the header
of a column and open a new dialog for setting the display format which
offers a (limited) list of pre-defined formats. The selected format is
then integrated into the SELECT statement which is sent to SQLite.

While it works, this draft implementation lacks a number of features.
Here are the most prominent ones I'm currently aware of:
* Data not editable (or only via the Edit Dialog) because it isn't
  transformed back yet.
* More display formats needed; maybe customizable ones, too.
* No indication in the UI for which columns a format has been set.
* Could _maybe_ be integrated into the import/export etc. for optional
  use.
This commit is contained in:
Martin Kleusberg
2015-07-06 18:28:30 +02:00
parent 944e22a80d
commit 7c1d237d9b
11 changed files with 349 additions and 10 deletions

View File

@@ -24,6 +24,7 @@ void SqliteTableModel::reset()
m_headers.clear();
m_mWhere.clear();
m_vDataTypes.clear();
m_vDisplayFormat.clear();
}
void SqliteTableModel::setChunkSize(size_t chunksize)
@@ -31,11 +32,12 @@ void SqliteTableModel::setChunkSize(size_t chunksize)
m_chunkSize = chunksize;
}
void SqliteTableModel::setTable(const QString& table)
void SqliteTableModel::setTable(const QString& table, const QVector<QString>& display_format)
{
reset();
m_sTable = table;
m_vDisplayFormat = display_format;
m_vDataTypes.push_back(SQLITE_INTEGER);
@@ -333,7 +335,17 @@ Qt::ItemFlags SqliteTableModel::flags(const QModelIndex& index) const
return Qt::ItemIsEnabled;
Qt::ItemFlags ret = QAbstractTableModel::flags(index);
if(!isBinary(index))
// Custom display format set?
bool custom_display_format = false;
if(m_vDisplayFormat.size())
{
// NOTE: This assumes that custom display formats never start and end with a backtick
if(index.column() > 0)
custom_display_format = !(m_vDisplayFormat.at(index.column()-1).startsWith("`") && m_vDisplayFormat.at(index.column()-1).endsWith("`"));
}
if(!isBinary(index) && !custom_display_format)
ret |= Qt::ItemIsEditable;
return ret;
}
@@ -469,7 +481,17 @@ void SqliteTableModel::buildQuery()
where.append(QString(" AND `%1` %2").arg(m_headers.at(i.key())).arg(i.value()));
}
QString sql = QString("SELECT `%1`,* FROM `%2` %3 ORDER BY `%4` %5").arg(m_headers.at(0)).arg(m_sTable).arg(where).arg(m_headers.at(m_iSortColumn)).arg(m_sSortOrder);
QString selector;
if(m_vDisplayFormat.empty())
{
selector = "*";
} else {
for(int i=0;i<m_vDisplayFormat.size();i++)
selector += m_vDisplayFormat.at(i) + ",";
selector.chop(1);
}
QString sql = QString("SELECT `%1`,%2 FROM `%3` %4 ORDER BY `%5` %6").arg(m_headers.at(0)).arg(selector).arg(m_sTable).arg(where).arg(m_headers.at(m_iSortColumn)).arg(m_sSortOrder);
setQuery(sql, true);
}