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

@@ -0,0 +1,59 @@
#include "ColumnDisplayFormatDialog.h"
#include "ui_ColumnDisplayFormatDialog.h"
ColumnDisplayFormatDialog::ColumnDisplayFormatDialog(const QString& colname, QString current_format, QWidget* parent)
: QDialog(parent),
ui(new Ui::ColumnDisplayFormatDialog),
column_name(colname)
{
// Create UI
ui->setupUi(this);
ui->comboDisplayFormat->addItem(tr("Default"), "default");
ui->comboDisplayFormat->addItem(tr("Lower case"), "lower");
ui->comboDisplayFormat->addItem(tr("Upper case"), "upper");
ui->comboDisplayFormat->addItem(tr("Unix epoch to date"), "epoch");
ui->comboDisplayFormat->addItem(tr("Julian day to date"), "julian");
ui->comboDisplayFormat->addItem(tr("Round number"), "round");
ui->labelDisplayFormat->setText(ui->labelDisplayFormat->text().arg(column_name));
// Set the current format, if it's empty set the default format
if(current_format.isEmpty())
{
ui->comboDisplayFormat->setCurrentIndex(0);
updateSqlCode();
} else {
ui->comboDisplayFormat->addItem(tr("Custom"), "custom");
ui->comboDisplayFormat->setCurrentIndex(ui->comboDisplayFormat->findData("custom"));
ui->editDisplayFormat->setText(current_format);
}
}
ColumnDisplayFormatDialog::~ColumnDisplayFormatDialog()
{
delete ui;
}
QString ColumnDisplayFormatDialog::selectedDisplayFormat() const
{
if(ui->comboDisplayFormat->currentData().toString() == "default")
return QString();
else
return ui->editDisplayFormat->text();
}
void ColumnDisplayFormatDialog::updateSqlCode()
{
QString format = ui->comboDisplayFormat->currentData().toString();
if(format == "default")
ui->editDisplayFormat->setText("`" + column_name + "`");
else if(format == "lower")
ui->editDisplayFormat->setText("lower(`" + column_name + "`)");
else if(format == "upper")
ui->editDisplayFormat->setText("upper(`" + column_name + "`)");
else if(format == "epoch")
ui->editDisplayFormat->setText("datetime(`" + column_name + "`, 'unixepoch')");
else if(format == "julian")
ui->editDisplayFormat->setText("datetime(`" + column_name + "`)");
else if(format == "round")
ui->editDisplayFormat->setText("round(`" + column_name + "`)");
}