• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

okteta

  • sources
  • kde-4.12
  • kdesdk
  • okteta
  • libs
  • kasten
  • controllers
  • view
  • version
versioncontroller.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the Kasten Framework, made within the KDE community.
3 
4  Copyright 2008 Friedrich W. H. Kossebau <kossebau@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) version 3, or any
10  later version accepted by the membership of KDE e.V. (or its
11  successor approved by the membership of KDE e.V.), which shall
12  act as a proxy defined in Section 6 of version 3 of the license.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include "versioncontroller.h"
24 
25 // Kasten core
26 #include <documentversiondata.h>
27 #include <versionable.h>
28 #include <abstractmodel.h>
29 // KDE
30 #include <KXMLGUIClient>
31 #include <KLocale>
32 #include <KActionCollection>
33 #include <KToolBarPopupAction>
34 #include <KStandardShortcut>
35 // Qt
36 #include <QtGui/QMenu>
37 
38 
39 namespace Kasten2
40 {
41 
42 static const int MaxMenuEntries = 10;
43 
44 VersionController::VersionController( KXMLGUIClient* guiClient )
45  : mModel( 0 )
46 {
47  KActionCollection* actionCollection = guiClient->actionCollection();
48 
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() );
52 
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*)) );
59 
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() );
63 
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*)) );
70 
71  setTargetModel( 0 );
72 }
73 
74 void VersionController::setTargetModel( AbstractModel* model )
75 {
76  if( mModel )
77  {
78  mModel->disconnect( this );
79  AbstractModel* versionedModel = mModel->findBaseModelWithInterface<If::Versionable*>();
80  if( versionedModel ) versionedModel->disconnect( this );
81  }
82 
83  mModel = model;
84  AbstractModel* versionedModel = mModel ? mModel->findBaseModelWithInterface<If::Versionable*>() : 0;
85  mVersionControl = versionedModel ? qobject_cast<If::Versionable*>( versionedModel ) : 0;
86 
87  if( mVersionControl )
88  {
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)) );
92  }
93  else
94  mModel = 0;
95 
96  const bool isVersionable = ( mVersionControl != 0 && !mModel->isReadOnly() );
97 
98  if( isVersionable )
99  onVersionIndexChanged( mVersionControl->versionIndex() );
100  else
101  {
102  mSetToOlderVersionAction->setEnabled( false );
103  mSetToNewerVersionAction->setEnabled( false );
104  }
105 }
106 
107 void VersionController::onVersionIndexChanged( int versionIndex )
108 {
109  const bool hasOlderVersions = ( versionIndex > 0 );
110  mSetToOlderVersionAction->setEnabled( hasOlderVersions );
111  if( hasOlderVersions )
112  mSetToOlderVersionAction->setData( versionIndex-1 );
113 
114  const bool hasNewerVersions = ( versionIndex < (mVersionControl->versionCount()-1) );
115  mSetToNewerVersionAction->setEnabled( hasNewerVersions );
116  if( hasNewerVersions )
117  mSetToNewerVersionAction->setData( versionIndex+1 );
118 }
119 
120 void VersionController::onSetToOlderVersionTriggered()
121 {
122  const int versionIndex = mSetToOlderVersionAction->data().toInt();
123  mVersionControl->revertToVersionByIndex( versionIndex );
124 }
125 
126 void VersionController::onSetToNewerVersionTriggered()
127 {
128  const int versionIndex = mSetToNewerVersionAction->data().toInt();
129  mVersionControl->revertToVersionByIndex( versionIndex );
130 }
131 
132 void VersionController::onOlderVersionMenuAboutToShow()
133 {
134  QMenu *menu = mSetToOlderVersionAction->menu();
135  menu->clear();
136 
137  int menuEntries = 0;
138  for( int versionIndex = mVersionControl->versionIndex();
139  versionIndex > 0 && menuEntries < MaxMenuEntries;
140  --versionIndex, ++menuEntries )
141  {
142  DocumentVersionData versionData = mVersionControl->versionData( versionIndex );
143 
144  const QString changeComment = versionData.changeComment();
145 
146  const QString actionText = i18nc( "@action Undo: [change]", "Undo: %1", changeComment );
147 
148  QAction *action = menu->addAction( actionText );
149  action->setData( versionIndex-1 );
150  }
151 }
152 
153 void VersionController::onNewerVersionMenuAboutToShow()
154 {
155  QMenu *menu = mSetToNewerVersionAction->menu();
156  menu->clear();
157 
158  int menuEntries = 0;
159  for( int versionIndex = mVersionControl->versionIndex()+1;
160  versionIndex < mVersionControl->versionCount() && menuEntries < MaxMenuEntries;
161  ++versionIndex, ++menuEntries )
162  {
163  DocumentVersionData versionData = mVersionControl->versionData( versionIndex );
164 
165  const QString changeComment = versionData.changeComment();
166 
167  const QString actionText = i18nc( "@action Redo: [change]", "Redo: %1", changeComment );
168 
169  QAction *action = menu->addAction( actionText );
170  action->setData( versionIndex );
171  }
172 }
173 
174 void VersionController::onOlderVersionMenuTriggered( QAction *action )
175 {
176  const int versionIndex = action->data().toInt();
177  mVersionControl->revertToVersionByIndex( versionIndex );
178 }
179 
180 void VersionController::onNewerVersionMenuTriggered( QAction *action )
181 {
182  const int versionIndex = action->data().toInt();
183  mVersionControl->revertToVersionByIndex( versionIndex );
184 }
185 
186 void VersionController::onReadOnlyChanged( bool isReadOnly )
187 {
188  if( isReadOnly )
189  {
190  mSetToOlderVersionAction->setEnabled( false );
191  mSetToNewerVersionAction->setEnabled( false );
192  }
193  else
194  onVersionIndexChanged( mVersionControl->versionIndex() );
195 }
196 
197 }
abstractmodel.h
Kasten2::MaxMenuEntries
static const int MaxMenuEntries
Definition: versioncontroller.cpp:42
Kasten2::If::Versionable::versionData
virtual DocumentVersionData versionData(int versionIndex) const =0
Kasten2::AbstractModel::findBaseModelWithInterface
AbstractModel * findBaseModelWithInterface() const
returns the first baseModel which is of type T, or null if none is found.
Definition: abstractmodel.h:109
Kasten2::If::Versionable
Definition: libs/kasten/core/document/versionable.h:68
Kasten2::If::Versionable::revertToVersionByIndex
virtual void revertToVersionByIndex(int versionIndex)=0
Kasten2::If::Versionable::versionIndex
virtual int versionIndex() const =0
Kasten2::If::Versionable::versionCount
virtual int versionCount() const =0
Kasten2::DocumentVersionData::changeComment
QString changeComment() const
Definition: documentversiondata.cpp:35
Kasten2::VersionController::VersionController
VersionController(KXMLGUIClient *guiClient)
Definition: versioncontroller.cpp:44
versioncontroller.h
Kasten2::AbstractModel
Definition: abstractmodel.h:40
Kasten2::AbstractModel::isReadOnly
virtual bool isReadOnly() const
default returns true
Definition: abstractmodel.cpp:39
Kasten2::VersionController::setTargetModel
virtual void setTargetModel(AbstractModel *model)
Definition: versioncontroller.cpp:74
documentversiondata.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:09 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okteta

Skip menu "okteta"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal