diff --git a/src/ExtendedTableWidget.cpp b/src/ExtendedTableWidget.cpp new file mode 100644 index 00000000..c4939758 --- /dev/null +++ b/src/ExtendedTableWidget.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include "ExtendedTableWidget.h" + +ExtendedTableWidget::ExtendedTableWidget(int rows, int columns, QWidget* parent) : + QTableWidget(rows, columns, parent) +{ +} + + +ExtendedTableWidget::ExtendedTableWidget(QWidget* parent) : + QTableWidget(parent) +{ +} + +void ExtendedTableWidget::copy() +{ + // Get list of selected items + QItemSelectionModel* selection = selectionModel(); + QModelIndexList indices = selection->selectedIndexes(); + + // Abort if there's nothing to copy + if(indices.size() == 0) + return; + + // Sort the items by row, then by column + qSort(indices); + + // Go through all the items... + QString result; + QModelIndex prev = indices.front(); + indices.removeFirst(); + foreach(QModelIndex index, indices) + { + // Add the content of this cell to the clipboard string + result.append(QString("\"%1\"").arg(prev.data().toString())); + + // If this is a new row add a line break, if not add a tab for cell separation + if(index.row() != prev.row()) + result.append("\r\n"); + else + result.append("\t"); + + prev = index; + } + result.append(QString("\"%1\"\r\n").arg(indices.last().data().toString())); // And the last cell + + // And finally add it to the clipboard + qApp->clipboard()->setText(result); +} + +void ExtendedTableWidget::keyPressEvent(QKeyEvent* event) +{ + // Call a custom copy method when Ctrl-C is pressed + if(event->matches(QKeySequence::Copy)) + copy(); + else + QTableWidget::keyPressEvent(event); +} diff --git a/src/ExtendedTableWidget.h b/src/ExtendedTableWidget.h new file mode 100644 index 00000000..e5a780e2 --- /dev/null +++ b/src/ExtendedTableWidget.h @@ -0,0 +1,21 @@ +#ifndef __EXTENDEDTABLEWIDGET_H__ +#define __EXTENDEDTABLEWIDGET_H__ + +#include + +class ExtendedTableWidget : public QTableWidget +{ + Q_OBJECT + +public: + explicit ExtendedTableWidget(int rows, int columns, QWidget* parent = 0); + explicit ExtendedTableWidget(QWidget* parent = 0); + +private: + void copy(); + +protected: + virtual void keyPressEvent(QKeyEvent* event); +}; + +#endif diff --git a/src/MainWindow.ui b/src/MainWindow.ui index 9f637c6a..b15587da 100644 --- a/src/MainWindow.ui +++ b/src/MainWindow.ui @@ -205,12 +205,12 @@ - + This is the database view. You can double-click any record to edit its contents in the cell editor window. - QAbstractItemView::SingleSelection + QAbstractItemView::ExtendedSelection QAbstractItemView::ScrollPerPixel @@ -316,8 +316,8 @@ 0 0 - 763 - 494 + 278 + 444 @@ -1399,6 +1399,11 @@ QTextEdit
sqltextedit.h
+ + ExtendedTableWidget + QTableWidget +
ExtendedTableWidget.h
+
dbTreeWidget @@ -2169,5 +2174,6 @@ deleteField() loadPragmas() savePragmas() + copy() diff --git a/src/src.pro b/src/src.pro index a3bdffe9..8eb6ec88 100644 --- a/src/src.pro +++ b/src/src.pro @@ -24,7 +24,8 @@ HEADERS += \ ExportCsvDialog.h \ ImportCsvDialog.h \ sqltextedit.h \ - sqlitetypes.h + sqlitetypes.h \ + ExtendedTableWidget.h SOURCES += \ sqlitedb.cpp \ @@ -41,7 +42,8 @@ SOURCES += \ ExportCsvDialog.cpp \ ImportCsvDialog.cpp \ sqltextedit.cpp \ - sqlitetypes.cpp + sqlitetypes.cpp \ + ExtendedTableWidget.cpp # create a unittest option CONFIG(unittest) {