summaryrefslogtreecommitdiff
path: root/src/main-view/DictTableView.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main-view/DictTableView.cpp')
-rw-r--r--src/main-view/DictTableView.cpp84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/main-view/DictTableView.cpp b/src/main-view/DictTableView.cpp
new file mode 100644
index 0000000..9d61fad
--- /dev/null
+++ b/src/main-view/DictTableView.cpp
@@ -0,0 +1,84 @@
+#include "DictTableView.h"
+#include "DictTableDelegate.h"
+
+#include <QtAlgorithms>
+#include <QHeaderView>
+#include <QAbstractProxyModel>
+
+#include "DictTableModel.h"
+
+DictTableView::DictTableView( QAbstractItemModel* aModel, QWidget* aParent ):
+ QTableView( aParent )
+ {
+ setModel( aModel );
+ connect( aModel, SIGNAL(rowsAboutToBeInserted(const QModelIndex&, int, int)),
+ SLOT(disableUpdates()) );
+ connect( aModel, SIGNAL(rowsInserted(const QModelIndex&, int, int)),
+ SLOT(enableUpdates()) );
+ setItemDelegate( new DictTableDelegate(this) );
+ setAlternatingRowColors(true);
+ setShowGrid(true);
+ verticalHeader()->setDefaultSectionSize( RowHeight );
+ setSelectionBehavior( QAbstractItemView::SelectRows );
+ resizeColumnsToContents();
+ horizontalHeader()->stretchLastSection();
+ }
+
+DictTableView::~DictTableView()
+ {
+ emit destroyed( model() );
+ }
+
+DictTableModel* DictTableView::dicTableModel() const
+ {
+ DictTableModel* tableModel = qobject_cast<DictTableModel*>( model() );
+ if( tableModel )
+ return tableModel;
+ QAbstractProxyModel* proxyModel = qobject_cast<QAbstractProxyModel*>( model() );
+ if( proxyModel )
+ {
+ tableModel = qobject_cast<DictTableModel*>( proxyModel->sourceModel() );
+ if( tableModel )
+ return tableModel;
+ }
+ return NULL;
+ }
+
+void DictTableView::resizeColumnsToContents()
+ {
+ QTableView::resizeColumnsToContents();
+ for( int i=0; i<model()->columnCount(); i++ )
+ if( columnWidth( i ) < KMinColWidth )
+ setColumnWidth( i, KMinColWidth );
+ else if ( columnWidth( i ) > KMaxColWidth )
+ setColumnWidth( i, KMaxColWidth );
+}
+
+void DictTableView::startEditing(int row, int col)
+{
+ edit(model()->index(row, col));
+}
+
+void DictTableView::commitEditing()
+{
+ DictTableDelegate* delegate = qobject_cast<DictTableDelegate*>(itemDelegate());
+ if(!delegate)
+ return;
+ commitData(delegate->getEditor());
+}
+
+int DictTableView::getEditorCursorPos() const
+{
+ DictTableDelegate* delegate = qobject_cast<DictTableDelegate*>(itemDelegate());
+ if(!delegate)
+ return -1;
+ return delegate->getCursorPos();
+}
+
+void DictTableView::insertImageIntoEditor(int cursorPos, const QString& filePath) const
+{
+ DictTableDelegate* delegate = qobject_cast<DictTableDelegate*>(itemDelegate());
+ if(!delegate)
+ return;
+ delegate->insertImageIntoEditor(cursorPos, filePath);
+}