|
18 | 18 | #include "ExportSqlDialog.h" |
19 | 19 | #include "SqlUiLexer.h" |
20 | 20 | #include "FileDialog.h" |
| 21 | +#include "ColumnDisplayFormatDialog.h" |
21 | 22 |
|
22 | 23 | #include <QFile> |
23 | 24 | #include <QApplication> |
@@ -116,6 +117,9 @@ void MainWindow::init() |
116 | 117 | popupSaveSqlFileMenu->addAction(ui->actionSqlSaveFileAs); |
117 | 118 | ui->actionSqlSaveFilePopup->setMenu(popupSaveSqlFileMenu); |
118 | 119 |
|
| 120 | + popupBrowseDataHeaderMenu = new QMenu(this); |
| 121 | + popupBrowseDataHeaderMenu->addAction(ui->actionBrowseTableEditDisplayFormat); |
| 122 | + |
119 | 123 | // Add menu item for log dock |
120 | 124 | ui->viewMenu->insertAction(ui->viewDBToolbarAction, ui->dockLog->toggleViewAction()); |
121 | 125 | ui->viewMenu->actions().at(0)->setShortcut(QKeySequence(tr("Ctrl+L"))); |
@@ -155,6 +159,7 @@ void MainWindow::init() |
155 | 159 | connect(editWin, SIGNAL(goingAway()), this, SLOT(editWinAway())); |
156 | 160 | connect(editWin, SIGNAL(updateRecordText(int, int, QByteArray)), this, SLOT(updateRecordText(int, int, QByteArray))); |
157 | 161 | connect(ui->dbTreeWidget->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(changeTreeSelection())); |
| 162 | + connect(ui->dataTable->horizontalHeader(), SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showDataColumnPopupMenu(QPoint))); |
158 | 163 |
|
159 | 164 | // Load window settings |
160 | 165 | tabifyDockWidget(ui->dockLog, ui->dockPlot); |
@@ -319,16 +324,41 @@ void MainWindow::populateTable(const QString& tablename) |
319 | 324 | // Set model |
320 | 325 | ui->dataTable->setModel(m_browseTableModel); |
321 | 326 |
|
| 327 | + // Search stored table settings for this table |
| 328 | + QMap<QString, BrowseDataTableSettings>::ConstIterator tableIt; |
| 329 | + tableIt = browseTableSettings.constFind(tablename); |
| 330 | + bool storedDataFound = tableIt != browseTableSettings.constEnd(); |
| 331 | + |
322 | 332 | // Set new table |
323 | | - m_browseTableModel->setTable(tablename); |
| 333 | + if(!storedDataFound) |
| 334 | + { |
| 335 | + m_browseTableModel->setTable(tablename); |
| 336 | + } else { |
| 337 | + QVector<QString> v; |
| 338 | + bool only_defaults = true; |
| 339 | + for(int i=0;i<db.getObjectByName(tablename).table.fields().size();i++) |
| 340 | + { |
| 341 | + QString format = tableIt.value().displayFormats[i+1]; |
| 342 | + if(format.size()) |
| 343 | + { |
| 344 | + v.push_back(format); |
| 345 | + only_defaults = false; |
| 346 | + } else { |
| 347 | + v.push_back("`" + db.getObjectByName(tablename).table.fields().at(i)->name() + "`"); |
| 348 | + } |
| 349 | + } |
| 350 | + if(only_defaults) |
| 351 | + m_browseTableModel->setTable(tablename); |
| 352 | + else |
| 353 | + m_browseTableModel->setTable(tablename, v); |
| 354 | + } |
324 | 355 | ui->dataTable->setColumnHidden(0, true); |
325 | 356 |
|
326 | 357 | // Update the filter row |
327 | 358 | qobject_cast<FilterTableHeader*>(ui->dataTable->horizontalHeader())->generateFilters(m_browseTableModel->columnCount()); |
328 | 359 |
|
329 | 360 | // Restore table settings |
330 | | - QMap<QString, BrowseDataTableSettings>::ConstIterator tableIt; |
331 | | - if((tableIt = browseTableSettings.constFind(tablename)) != browseTableSettings.constEnd()) |
| 361 | + if(storedDataFound) |
332 | 362 | { |
333 | 363 | // There is information stored for this table, so extract it and apply it |
334 | 364 |
|
@@ -2176,3 +2206,41 @@ void MainWindow::jumpToRow(const QString& table, QString column, const QByteArra |
2176 | 2206 | // Set filter |
2177 | 2207 | ui->dataTable->filterHeader()->setFilter(column_index+1, value); |
2178 | 2208 | } |
| 2209 | + |
| 2210 | +void MainWindow::showDataColumnPopupMenu(const QPoint& pos) |
| 2211 | +{ |
| 2212 | + // Get the index of the column which the user has clicked on and store it in the action. This is sort of hack-ish and it might be the heat in my room |
| 2213 | + // but I haven't come up with a better solution so far |
| 2214 | + ui->actionBrowseTableEditDisplayFormat->setProperty("clicked_column", ui->dataTable->horizontalHeader()->logicalIndexAt(pos)); |
| 2215 | + |
| 2216 | + // Calculate the proper position for the context menu and display it |
| 2217 | + popupBrowseDataHeaderMenu->exec(ui->dataTable->horizontalHeader()->mapToGlobal(pos)); |
| 2218 | +} |
| 2219 | + |
| 2220 | +void MainWindow::editDataColumnDisplayFormat() |
| 2221 | +{ |
| 2222 | + // Get the current table name and fetch its table object, then retrieve the fields of that table and look up the index of the clicked table header |
| 2223 | + // section using it as the table field array index. Subtract one from the header index to get the column index because in the the first (though hidden) |
| 2224 | + // column is always the rowid column. Ultimately, get the column name from the column object |
| 2225 | + QString current_table = ui->comboBrowseTable->currentText(); |
| 2226 | + int field_number = sender()->property("clicked_column").toInt(); |
| 2227 | + QString field_name = db.getObjectByName(current_table).table.fields().at(field_number-1)->name(); |
| 2228 | + |
| 2229 | + // Get the current display format of the field |
| 2230 | + QString current_displayformat = browseTableSettings[current_table].displayFormats[field_number]; |
| 2231 | + |
| 2232 | + // Open the dialog |
| 2233 | + ColumnDisplayFormatDialog dialog(field_name, current_displayformat, this); |
| 2234 | + if(dialog.exec()) |
| 2235 | + { |
| 2236 | + // Set the newly selected display format |
| 2237 | + QString new_format = dialog.selectedDisplayFormat(); |
| 2238 | + if(new_format.size()) |
| 2239 | + browseTableSettings[current_table].displayFormats[field_number] = new_format; |
| 2240 | + else |
| 2241 | + browseTableSettings[current_table].displayFormats.remove(field_number); |
| 2242 | + |
| 2243 | + // Refresh view |
| 2244 | + populateTable(current_table); |
| 2245 | + } |
| 2246 | +} |
0 commit comments