Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retain display formats #1202

Merged
merged 9 commits into from
Nov 2, 2017
73 changes: 40 additions & 33 deletions src/ColumnDisplayFormatDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,59 @@ ColumnDisplayFormatDialog::ColumnDisplayFormatDialog(const QString& colname, QSt
// Create UI
ui->setupUi(this);
ui->comboDisplayFormat->addItem(tr("Default"), "default");
ui->comboDisplayFormat->addItem(tr("Apple NSDate to date"), "appleDate");
ui->comboDisplayFormat->insertSeparator(ui->comboDisplayFormat->count());
ui->comboDisplayFormat->addItem(tr("Decimal number"), "decimal");
ui->comboDisplayFormat->addItem(tr("Exponent notation"), "exponent");
ui->comboDisplayFormat->addItem(tr("Hex blob"), "hexblob");
ui->comboDisplayFormat->addItem(tr("Hex number"), "hex");
ui->comboDisplayFormat->addItem(tr("Java epoch (milliseconds) to date"), "javaEpoch");
ui->comboDisplayFormat->addItem(tr("Julian day to date"), "julian");
ui->comboDisplayFormat->addItem(tr("Lower case"), "lower");
ui->comboDisplayFormat->addItem(tr("Octal number"), "octal");
ui->comboDisplayFormat->addItem(tr("Round number"), "round");
ui->comboDisplayFormat->insertSeparator(ui->comboDisplayFormat->count());
ui->comboDisplayFormat->addItem(tr("Apple NSDate to date"), "appleDate");
ui->comboDisplayFormat->addItem(tr("Java epoch (milliseconds) to date"), "javaEpoch");
ui->comboDisplayFormat->addItem(tr("Julian day to date"), "julian");
ui->comboDisplayFormat->addItem(tr("Unix epoch to date"), "epoch");
ui->comboDisplayFormat->addItem(tr("Upper case"), "upper");
ui->comboDisplayFormat->addItem(tr("Windows DATE to date"), "winDate");
ui->comboDisplayFormat->insertSeparator(ui->comboDisplayFormat->count());
ui->comboDisplayFormat->addItem(tr("Lower case"), "lower");
ui->comboDisplayFormat->addItem(tr("Upper case"), "upper");
ui->labelDisplayFormat->setText(ui->labelDisplayFormat->text().arg(column_name));

formatFunctions["lower"] = "lower(" + sqlb::escapeIdentifier(column_name) + ")";
formatFunctions["upper"] = "upper(" + sqlb::escapeIdentifier(column_name) + ")";
formatFunctions["epoch"] = "datetime(" + sqlb::escapeIdentifier(column_name) + ", 'unixepoch')";
formatFunctions["javaEpoch"] = "strftime('%Y-%m-%d %H:%M:%S.', " + sqlb::escapeIdentifier(column_name) +
"/1000, 'unixepoch') || (" + sqlb::escapeIdentifier(column_name) + "%1000)";
formatFunctions["winDate"] = "datetime('1899-12-30', " + sqlb::escapeIdentifier(column_name) + " || \" days\")";
formatFunctions["appleDate"] = "datetime('2001-01-01', " + sqlb::escapeIdentifier(column_name) + " || \" seconds\")";
formatFunctions["julian"] = "datetime(" + sqlb::escapeIdentifier(column_name) + ")";
formatFunctions["round"] = "round(" + sqlb::escapeIdentifier(column_name) + ")";
formatFunctions["hex"] = "printf('0x%x', " + sqlb::escapeIdentifier(column_name) + ")";
formatFunctions["octal"] = "printf('%o', " + sqlb::escapeIdentifier(column_name) + ")";
formatFunctions["exponent"] = "printf('%e', " + sqlb::escapeIdentifier(column_name) + ")";
formatFunctions["hexblob"] = "hex(" + sqlb::escapeIdentifier(column_name) + ")";
formatFunctions["decimal"] = "printf('%d', " + sqlb::escapeIdentifier(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"));
QString formatName;
foreach (const QString &formatKey, formatFunctions.keys()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also use a range-based for loop from C++11 here since we don't have to support older compilers anymore. The foreach provided by Qt is slower than the C++11 one because it creates a copy of the entire map before iterating over the copy. It's not a big deal though in this case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect. I've committed the improvement.

if(current_format == formatFunctions.value(formatKey)) {
formatName = formatKey;
break;
}
}

if(formatName.isEmpty()) {
ui->comboDisplayFormat->insertSeparator(ui->comboDisplayFormat->count());
ui->comboDisplayFormat->addItem(tr("Custom"), "custom");
formatName = "custom";
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one minor thing: here and above in the foreach loop you're mixing spaces and tabs for indentation which makes the indentation look wrong if the tab width is not set to 8 spaces. Can you change the tabs to 4 spaces instead? 😄 By the way, if you're using Qt Creator you can go to the "Projects" panel (big button on the left), there click "Editor" in the "Project Settings" section, and then set the Tab policy to "Spaces only", the Tab size to "4" and the Indent Size to "4". That changes it just for the sqlitebrowser project and keeps the old settings for all your other projects. That's how I configured it, too 😉

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't aware that Emacs was inserting spaces. I've already changed its settings, so it no longer does that. I don't use Qt Creator for the moment, but I will give it a try.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're used to Emacs, I'd stick with that (and I say that as a Vim-person 😉) but Qt Creator is definitely worth giving a try, especially when editing the .ui files 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some tasks are easier with those visual IDEs, but I know too many Emacs tricks and powerful commands for just abandoning it 😄

ui->comboDisplayFormat->setCurrentIndex(ui->comboDisplayFormat->findData(formatName));
ui->editDisplayFormat->setText(current_format);
}
}
Expand All @@ -56,30 +86,7 @@ void ColumnDisplayFormatDialog::updateSqlCode()

if(format == "default")
ui->editDisplayFormat->setText(sqlb::escapeIdentifier(column_name));
else if(format == "lower")
ui->editDisplayFormat->setText("lower(" + sqlb::escapeIdentifier(column_name) + ")");
else if(format == "upper")
ui->editDisplayFormat->setText("upper(" + sqlb::escapeIdentifier(column_name) + ")");
else if(format == "epoch")
ui->editDisplayFormat->setText("datetime(" + sqlb::escapeIdentifier(column_name) + ", 'unixepoch')");
else if(format == "javaEpoch")
ui->editDisplayFormat->setText("strftime('%Y-%m-%d %H:%M:%S.', " + sqlb::escapeIdentifier(column_name) + "/1000, 'unixepoch') || (" + sqlb::escapeIdentifier(column_name) + "%1000)");
else if(format == "winDate")
ui->editDisplayFormat->setText("datetime ('1899-12-30', " + sqlb::escapeIdentifier(column_name) + " || \" days\" )");
else if(format == "appleDate")
ui->editDisplayFormat->setText("datetime ('2001-01-01', " + sqlb::escapeIdentifier(column_name) + " || \" seconds\" )");
else if(format == "julian")
ui->editDisplayFormat->setText("datetime(" + sqlb::escapeIdentifier(column_name) + ")");
else if(format == "round")
ui->editDisplayFormat->setText("round(" + sqlb::escapeIdentifier(column_name) + ")");
else if(format == "hex")
ui->editDisplayFormat->setText("printf('0x%x', " + sqlb::escapeIdentifier(column_name) + ")");
else if(format == "octal")
ui->editDisplayFormat->setText("printf('%o', " + sqlb::escapeIdentifier(column_name) + ")");
else if(format == "exponent")
ui->editDisplayFormat->setText("printf('%e', " + sqlb::escapeIdentifier(column_name) + ")");
else if(format == "hexblob")
ui->editDisplayFormat->setText("hex(" + sqlb::escapeIdentifier(column_name) + ")");
else if(format == "decimal")
ui->editDisplayFormat->setText("printf('%d', " + sqlb::escapeIdentifier(column_name) + ")");
else
ui->editDisplayFormat->setText(formatFunctions.value(format));

}
3 changes: 3 additions & 0 deletions src/ColumnDisplayFormatDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define COLUMNDISPLAYFORMATDIALOG_H

#include <QDialog>
#include <QString>
#include <QMap>

namespace Ui {
class ColumnDisplayFormatDialog;
Expand All @@ -23,6 +25,7 @@ private slots:
private:
Ui::ColumnDisplayFormatDialog* ui;
QString column_name;
QMap<QString, QString> formatFunctions;
};

#endif