#include "DictionaryTabWidget.h" #include "MainWindow.h" #include "DictTableModel.h" #include "DictTableView.h" #include "../dictionary/Dictionary.h" DictionaryTabWidget::DictionaryTabWidget(MainWindow* aMainWin): QTabWidget( aMainWin ), m_mainWin( aMainWin ), createdEditorsNum(0) { setDocumentMode( true ); setTabsClosable( true ); setMovable( true ); connect( this, SIGNAL(tabCloseRequested(int)), SLOT(closeTab(int)) ); m_undoGroup = new QUndoGroup( this ); m_continueLbl = new QLabel( this ); m_continueLbl->hide(); m_continueLbl->setPixmap(QPixmap(":/images/continue-search.png")); } /** The tab is removed automatically when the widget is destroyed. The undo stack is removed from group automatically, when the stack is destroyed. */ void DictionaryTabWidget::closeTab( int aIndex ) { if( aIndex == -1 ) aIndex = currentIndex(); bool canRemove = m_mainWin->proposeToSave( aIndex ); if( canRemove ) delete widget( aIndex ); } int DictionaryTabWidget::addDictTab( DictTableModel* aDictModel ) { DictTableView* dictView = new DictTableView( aDictModel ); int tabIx = addTab( dictView, "" ); setCurrentIndex( tabIx ); QUndoStack* undoStack = aDictModel->undoStack(); m_undoGroup->addStack( undoStack ); m_undoGroup->setActiveStack( undoStack ); connect( undoStack, SIGNAL(cleanChanged(bool)), aDictModel->dictionary(), SLOT(setContentClean(bool)) ); connect( dictView->itemDelegate(), SIGNAL(editorCreated()), SLOT(createEditor()) ); connect( dictView->itemDelegate(), SIGNAL(editorDestroyed()), SLOT(destroyEditor()) ); return tabIx; } const DictTableView* DictionaryTabWidget::curDictView() const { QWidget* curWidget = currentWidget(); if( !curWidget ) return NULL; DictTableView* curView = static_cast( curWidget ); return curView; } void DictionaryTabWidget::setCurrentIndex( int aTabIx ) { QTabWidget::setCurrentIndex( aTabIx ); const DictTableView* dictView = curDictView(); if( dictView ) { const DictTableModel* dictModel = dictView->dicTableModel(); if( dictModel ) m_undoGroup->setActiveStack( dictModel->undoStack() ); } } void DictionaryTabWidget::goToDictionaryRecord( int aDictIx, int aRecordRow ) { setCurrentIndex( aDictIx ); QWidget* curWidget = currentWidget(); Q_ASSERT( curWidget ); QAbstractItemView* curView = static_cast( curWidget ); Q_ASSERT( curView ); curView->setFocus(); QModelIndex index = curView->model()->index( aRecordRow, 0 ); curView->setCurrentIndex( index ); } void DictionaryTabWidget::cleanUndoStack() { m_undoGroup->activeStack()->setClean(); } bool DictionaryTabWidget::undoStackIsClean() const { return m_undoGroup->isClean(); } void DictionaryTabWidget::showContinueSearch() { m_continueLbl->move( rect().center() - m_continueLbl->rect().center() ); m_continueLbl->show(); QTimer::singleShot( 500, m_continueLbl, SLOT(hide()) ); } void DictionaryTabWidget::createEditor() { createdEditorsNum++; emit editingStateChanged(); } void DictionaryTabWidget::destroyEditor() { createdEditorsNum--; emit editingStateChanged(); }