27 #include <versionable.h>
30 #include <KXMLGUIClient>
32 #include <KActionCollection>
33 #include <KToolBarPopupAction>
34 #include <KStandardShortcut>
36 #include <QtGui/QMenu>
47 KActionCollection* actionCollection = guiClient->actionCollection();
49 mSetToOlderVersionAction =
new KToolBarPopupAction( KIcon( QLatin1String(
"edit-undo") ), i18nc(
"@action:inmenu",
"Undo"),
this );
50 actionCollection->addAction( QLatin1String(
"edit_undo"), mSetToOlderVersionAction );
51 mSetToOlderVersionAction->setShortcuts( KStandardShortcut::undo() );
53 connect( mSetToOlderVersionAction, SIGNAL(triggered(
bool)),
54 SLOT(onSetToOlderVersionTriggered()) );
55 connect( mSetToOlderVersionAction->menu(), SIGNAL(aboutToShow()),
56 SLOT(onOlderVersionMenuAboutToShow()) );
57 connect( mSetToOlderVersionAction->menu(), SIGNAL(triggered(QAction*)),
58 SLOT(onOlderVersionMenuTriggered(QAction*)) );
60 mSetToNewerVersionAction =
new KToolBarPopupAction( KIcon( QLatin1String(
"edit-redo") ), i18nc(
"@action:inmenu",
"Redo"),
this );
61 actionCollection->addAction( QLatin1String(
"edit_redo"), mSetToNewerVersionAction );
62 mSetToNewerVersionAction->setShortcuts( KStandardShortcut::redo() );
64 connect( mSetToNewerVersionAction, SIGNAL(triggered(
bool)),
65 SLOT(onSetToNewerVersionTriggered()) );
66 connect( mSetToNewerVersionAction->menu(), SIGNAL(aboutToShow()),
67 SLOT(onNewerVersionMenuAboutToShow()) );
68 connect( mSetToNewerVersionAction->menu(), SIGNAL(triggered(QAction*)),
69 SLOT(onNewerVersionMenuTriggered(QAction*)) );
78 mModel->disconnect(
this );
80 if( versionedModel ) versionedModel->disconnect(
this );
85 mVersionControl = versionedModel ? qobject_cast<
If::Versionable*>( versionedModel ) : 0;
89 connect( versionedModel, SIGNAL(revertedToVersionIndex(
int)), SLOT(onVersionIndexChanged(
int)) );
90 connect( versionedModel, SIGNAL(headVersionChanged(
int)), SLOT(onVersionIndexChanged(
int)) );
91 connect( mModel, SIGNAL(readOnlyChanged(
bool)), SLOT(onReadOnlyChanged(
bool)) );
96 const bool isVersionable = ( mVersionControl != 0 && !mModel->
isReadOnly() );
99 onVersionIndexChanged( mVersionControl->
versionIndex() );
102 mSetToOlderVersionAction->setEnabled(
false );
103 mSetToNewerVersionAction->setEnabled(
false );
107 void VersionController::onVersionIndexChanged(
int versionIndex )
109 const bool hasOlderVersions = ( versionIndex > 0 );
110 mSetToOlderVersionAction->setEnabled( hasOlderVersions );
111 if( hasOlderVersions )
112 mSetToOlderVersionAction->setData( versionIndex-1 );
114 const bool hasNewerVersions = ( versionIndex < (mVersionControl->
versionCount()-1) );
115 mSetToNewerVersionAction->setEnabled( hasNewerVersions );
116 if( hasNewerVersions )
117 mSetToNewerVersionAction->setData( versionIndex+1 );
120 void VersionController::onSetToOlderVersionTriggered()
122 const int versionIndex = mSetToOlderVersionAction->data().toInt();
126 void VersionController::onSetToNewerVersionTriggered()
128 const int versionIndex = mSetToNewerVersionAction->data().toInt();
132 void VersionController::onOlderVersionMenuAboutToShow()
134 QMenu *menu = mSetToOlderVersionAction->menu();
138 for(
int versionIndex = mVersionControl->
versionIndex();
140 --versionIndex, ++menuEntries )
142 DocumentVersionData versionData = mVersionControl->
versionData( versionIndex );
146 const QString actionText = i18nc(
"@action Undo: [change]",
"Undo: %1", changeComment );
148 QAction *action = menu->addAction( actionText );
149 action->setData( versionIndex-1 );
153 void VersionController::onNewerVersionMenuAboutToShow()
155 QMenu *menu = mSetToNewerVersionAction->menu();
159 for(
int versionIndex = mVersionControl->
versionIndex()+1;
161 ++versionIndex, ++menuEntries )
163 DocumentVersionData versionData = mVersionControl->
versionData( versionIndex );
167 const QString actionText = i18nc(
"@action Redo: [change]",
"Redo: %1", changeComment );
169 QAction *action = menu->addAction( actionText );
170 action->setData( versionIndex );
174 void VersionController::onOlderVersionMenuTriggered( QAction *action )
176 const int versionIndex = action->data().toInt();
180 void VersionController::onNewerVersionMenuTriggered( QAction *action )
182 const int versionIndex = action->data().toInt();
186 void VersionController::onReadOnlyChanged(
bool isReadOnly )
190 mSetToOlderVersionAction->setEnabled(
false );
191 mSetToNewerVersionAction->setEnabled(
false );
194 onVersionIndexChanged( mVersionControl->
versionIndex() );
static const int MaxMenuEntries
virtual DocumentVersionData versionData(int versionIndex) const =0
AbstractModel * findBaseModelWithInterface() const
returns the first baseModel which is of type T, or null if none is found.
virtual void revertToVersionByIndex(int versionIndex)=0
virtual int versionIndex() const =0
virtual int versionCount() const =0
QString changeComment() const
VersionController(KXMLGUIClient *guiClient)
virtual bool isReadOnly() const
default returns true
virtual void setTargetModel(AbstractModel *model)