From 24b3fb54fd06d794a1cb88b89906a0dbfdbde691 Mon Sep 17 00:00:00 2001 From: nds Date: Fri, 28 Sep 2007 05:34:09 +0000 Subject: [PATCH] Copy/Paste functionality. --- src/TableViewer/TableViewer_ViewWindow.cxx | 123 ++++++++++++++++++++- src/TableViewer/TableViewer_ViewWindow.h | 22 +++- 2 files changed, 140 insertions(+), 5 deletions(-) diff --git a/src/TableViewer/TableViewer_ViewWindow.cxx b/src/TableViewer/TableViewer_ViewWindow.cxx index 0f50c3537..fdf002798 100755 --- a/src/TableViewer/TableViewer_ViewWindow.cxx +++ b/src/TableViewer/TableViewer_ViewWindow.cxx @@ -38,6 +38,8 @@ #include #include #include +#include +#include /*! Constructor @@ -48,6 +50,8 @@ TableViewer_ViewWindow::TableViewer_ViewWindow( SUIT_Desktop* theDesktop, { myModel = theModel; myTable = new QtxTable( this ); + connect( myTable->selectionModel(), SIGNAL( selectionChanged( const QItemSelection&, + const QItemSelection& ) ), this, SLOT( selectionChanged() ) ); //myTable->setReadOnly( true ); setCentralWidget( myTable ); @@ -113,6 +117,10 @@ void TableViewer_ViewWindow::createActions() resMgr->loadPixmap( "STD", tr( "ICON_EDIT_COPY" ) ), tr( "TIP_COPY" ), tr( "PRP_COPY" ) ); + createAction( PasteId, tr( "MEN_PASTE" ), + resMgr->loadPixmap( "STD", tr( "ICON_EDIT_PASTE" ) ), + tr( "TIP_PASTE" ), tr( "PRP_PASTE" ) ); + createAction( PrintId, tr( "MEN_PRINT" ), resMgr->loadPixmap( "STD", tr( "ICON_PRINT" ) ), tr( "TIP_PRINT" ), tr( "PRP_PRINT" ) ); @@ -176,14 +184,13 @@ QString TableViewer_ViewWindow::text( const ContentType type, const int row, con return aTxt; } -QString TableViewer_ViewWindow::image( const ContentType type, const int row, const int col ) const +QString TableViewer_ViewWindow::image( const ContentType, const int, const int ) const { return ""; } -int TableViewer_ViewWindow::fontFlags( const ContentType type, const int row, const int col ) const +QFont TableViewer_ViewWindow::font( const ContentType type, const int row, const int col ) const { - int aFlags = 0; QFont aFont = myTable->font(); switch ( type ) { case VerticalHeader: @@ -198,6 +205,13 @@ int TableViewer_ViewWindow::fontFlags( const ContentType type, const int row, co default: break; } + return aFont; +} + +int TableViewer_ViewWindow::fontFlags( const ContentType type, const int row, const int col ) const +{ + int aFlags = 0; + QFont aFont = font( type, row, col ); if ( aFont.bold() ) aFlags |= HTMLService_HTMLText::Bold; if ( aFont.italic() ) @@ -206,7 +220,7 @@ int TableViewer_ViewWindow::fontFlags( const ContentType type, const int row, co aFlags |= HTMLService_HTMLText::Underline; if ( aFont.strikeOut() ) aFlags |= HTMLService_HTMLText::StrikeOut; - //Subscript + // 'Subscript' type is absent for export in HTML file return aFlags; } @@ -257,6 +271,7 @@ void TableViewer_ViewWindow::createToolBar() { myToolBar->addAction( myActionsMap[DumpId] ); myToolBar->addAction( myActionsMap[CopyId] ); + myToolBar->addAction( myActionsMap[PasteId] ); myToolBar->addAction( myActionsMap[PrintId] ); myToolBar->addAction( myActionsMap[ExportId] ); } @@ -270,6 +285,14 @@ void TableViewer_ViewWindow::actionActivated( const int id ) case ExportId: exportData(); break; + case CopyId: + copyData(); + break; + case PasteId: + pasteData(); + break; + default: + break; } } @@ -293,6 +316,13 @@ QtxAction* TableViewer_ViewWindow::createAction( const int id, const QString& me return a; } +void TableViewer_ViewWindow::selectionChanged() +{ + bool anEnable = myTable->getSelectedIndexes().count() > 0; + myActionsMap[CopyId]->setEnabled( anEnable ); + myActionsMap[PasteId]->setEnabled( anEnable & myCopyLst.count() > 0 ); +} + void TableViewer_ViewWindow::onActivated() { const QObject* obj = sender(); @@ -353,6 +383,80 @@ void TableViewer_ViewWindow::exportData() QApplication::restoreOverrideCursor(); } +void TableViewer_ViewWindow::copyData() +{ + QModelIndexList anItems = myTable->getSelectedIndexes(); + if ( anItems.count() <= 0 ) + return; + int aLeftCol = myTable->columnCount(), aTopRow = myTable->rowCount(); + QModelIndexList::const_iterator anIt = anItems.begin(), aLast = anItems.end(); + int aRow, aCol; + for ( ; anIt != aLast; ++anIt ) { + aRow = (*anIt).row(); + aCol = (*anIt).column(); + if ( !canPaste( aRow, aCol, "" ) ) + continue; + if ( aCol < aLeftCol ) + aLeftCol = aCol; + if ( aRow < aTopRow ) + aTopRow = aRow; + } + myCopyLst.clear(); + TableDataItem aCopyItem; + for ( anIt = anItems.begin(); anIt != aLast; ++anIt ) { + aRow = (*anIt).row(); + aCol = (*anIt).column(); + if ( !canCopy( aRow, aCol ) ) + continue; + aCopyItem.myRow = aRow-aTopRow; + aCopyItem.myCol = aCol-aLeftCol; + aCopyItem.myText = text( Cells, aRow, aCol ); + aCopyItem.myBgCol = backgroundColor( Cells, aRow, aCol ); + aCopyItem.myFgCol = foregroundColor( Cells, aRow, aCol ); + aCopyItem.myFont = font( Cells, aRow, aCol ); + myCopyLst.append( aCopyItem ); + } +} + +void TableViewer_ViewWindow::pasteData() +{ + QModelIndexList anItems = myTable->getSelectedIndexes(); + if ( anItems.count() <= 0 || myCopyLst.count() <= 0 ) + return; + int aLeftCol = myTable->columnCount(), aTopRow = myTable->rowCount(); + QModelIndexList::const_iterator anIt = anItems.begin(), aLast = anItems.end(); + QTableWidgetItem* anItem; + int aCol, aRow; + for ( ; anIt != aLast; ++anIt ) { + aRow = (*anIt).row(); + aCol = (*anIt).column(); + if ( !canPaste( aRow, aCol, "" ) ) + continue; + if ( aCol < aLeftCol ) + aLeftCol = aCol; + if ( aRow < aTopRow ) + aTopRow = aRow; + } + QList::const_iterator aCopyIt = myCopyLst.begin(), + aCopyLast = myCopyLst.end(); + //int aCol, aRow; + TableDataItem aCopyItem; + for ( ; aCopyIt != aCopyLast; aCopyIt++ ) { + aCopyItem = *aCopyIt; + aCol = aCopyItem.myCol+aLeftCol; + aRow = aCopyItem.myRow+aTopRow; + if ( !canPaste( aRow, aCol, aCopyItem.myText ) ) + continue; + anItem = myTable->getItem( aRow, aCol, true ); + anItem->setText( aCopyItem.myText ); + if ( aCopyItem.myBgCol.isValid() ) + anItem->setBackground( aCopyItem.myBgCol ); + if ( aCopyItem.myFgCol.isValid() ) + anItem->setForeground( aCopyItem.myFgCol ); + anItem->setFont( aCopyItem.myFont ); + } +} + void TableViewer_ViewWindow::exportTableData( Handle(HTMLService_HTMLTable)& table, const ContentType type, const int rowOffset, const int colOffset ) @@ -400,3 +504,14 @@ void TableViewer_ViewWindow::exportTableData( Handle(HTMLService_HTMLTable)& tab } } } + +bool TableViewer_ViewWindow::canCopy( const int theRow, const int theCol ) +{ + return true; +} + +bool TableViewer_ViewWindow::canPaste( const int theRow, const int theCol, const QString& ) +{ + return theRow < myTable->rowCount() && theRow >= 0 && + theCol < myTable->columnCount() & theCol >= 0; +} diff --git a/src/TableViewer/TableViewer_ViewWindow.h b/src/TableViewer/TableViewer_ViewWindow.h index 9a2f5e1c8..a346910e3 100755 --- a/src/TableViewer/TableViewer_ViewWindow.h +++ b/src/TableViewer/TableViewer_ViewWindow.h @@ -30,8 +30,11 @@ class SUIT_ViewWindow; class SUIT_Desktop; class Handle(HTMLService_HTMLTable); class QImage; +class QFont; class QtxTable; class QToolBar; +class QTableWidgetItem; +class QItemSelection; class TABLEVIEWER_EXPORT TableViewer_ViewWindow: public SUIT_ViewWindow { @@ -48,7 +51,7 @@ public: protected: typedef enum { VerticalHeader, HorizontalHeader, Cells } ContentType; - typedef enum { DumpId, CopyId, PrintId, ExportId, Custom } ActionId; + typedef enum { DumpId, CopyId, PasteId, PrintId, ExportId, Custom } ActionId; typedef QMap ActionsMap; virtual void createActions(); @@ -56,6 +59,8 @@ protected: virtual void actionActivated( const int ); virtual void exportTableData( Handle(HTMLService_HTMLTable)&, const ContentType, const int, const int ); + virtual bool canCopy( const int, const int ); + virtual bool canPaste( const int, const int, const QString& ); void registerAction( const int, QtxAction* ); QtxAction* createAction( const int, const QString&, const QPixmap&, const QString&, @@ -66,15 +71,29 @@ protected: QString text( const ContentType, const int, const int ) const; QString image( const ContentType, const int, const int ) const; + QFont font( const ContentType, const int, const int ) const; int fontFlags( const ContentType, const int, const int ) const; QColor foregroundColor( const ContentType, const int, const int ) const; QColor backgroundColor( const ContentType, const int, const int ) const; +protected slots: + virtual void selectionChanged(); + private slots: void onActivated(); private: void exportData(); + void copyData(); + void pasteData(); + typedef struct { + QString myText; + QColor myBgCol; + QColor myFgCol; + QFont myFont; + int myRow; + int myCol; + } TableDataItem; protected: TableViewer_Viewer* myModel; @@ -83,6 +102,7 @@ protected: private: QtxTable* myTable; QToolBar* myToolBar; + QList myCopyLst; }; #endif // !defined(TABLEVIEWER_VIEWWINDOW_H) -- 2.39.2