• 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
  • io
  • clipboard
clipboardcontroller.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 2006-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 "clipboardcontroller.h"
24 
25 // Kasten gui
26 #include <dataselectable.h>
27 #include <selecteddatawriteable.h>
28 // Kasten core
29 #include <abstractmodel.h>
30 // KDE
31 #include <KXMLGUIClient>
32 #include <KLocale>
33 #include <KAction>
34 #include <KActionCollection>
35 #include <KStandardAction>
36 // Qt
37 #include <QtGui/QClipboard>
38 #include <QtGui/QApplication>
39 #include <QtCore/QMimeData>
40 
41 
42 namespace Kasten2
43 {
44 
45 ClipboardController::ClipboardController( KXMLGUIClient* guiClient )
46  : mModel( 0 ), mSelectionControl( 0 ), mMimeDataControl( 0 )
47 {
48  KActionCollection* actionCollection = guiClient->actionCollection();
49 
50  mCutAction = KStandardAction::cut( this, SLOT(cut()), actionCollection );
51  mCopyAction = KStandardAction::copy( this, SLOT(copy()), actionCollection );
52  mPasteAction = KStandardAction::paste( this, SLOT(paste()), actionCollection );
53 
54  connect( QApplication::clipboard(), SIGNAL(dataChanged()), SLOT(onClipboardDataChanged()) );
55 
56  setTargetModel( 0 );
57 }
58 
59 void ClipboardController::setTargetModel( AbstractModel* model )
60 {
61  if( mModel ) mModel->disconnect( this );
62 
63  mModel = model ? model->findBaseModelWithInterface<If::DataSelectable*>() : 0;
64  mSelectionControl = mModel ? qobject_cast<If::DataSelectable *>( mModel ) : 0;
65 
66  if( mSelectionControl )
67  {
68  connect( mModel, SIGNAL(hasSelectedDataChanged(bool)), SLOT(onHasSelectedDataChanged(bool)) );
69 
70  mMimeDataControl = qobject_cast<If::SelectedDataWriteable*>( mModel );
71  if( mMimeDataControl )
72  connect( mModel, SIGNAL(readOnlyChanged(bool)), SLOT(onReadOnlyChanged(bool)) );
73  }
74  else
75  mMimeDataControl = 0;
76 
77  const QMimeData* mimeData = QApplication::clipboard()->mimeData( QClipboard::Clipboard );
78 
79  const bool hasSelectedData = ( mSelectionControl != 0 ) ? mSelectionControl->hasSelectedData() : false;
80  const bool isWriteable = ( mMimeDataControl != 0 && !mModel->isReadOnly() );
81  const bool isPastable = isWriteable && ! mimeData->formats().isEmpty() && mMimeDataControl->canReadData( mimeData );
82 
83  mCopyAction->setEnabled( hasSelectedData );
84  mCutAction->setEnabled( hasSelectedData && isWriteable );
85  mPasteAction->setEnabled( isPastable );
86 }
87 
88 void ClipboardController::cut()
89 {
90  QMimeData *data = mMimeDataControl->cutSelectedData();
91  if( !data )
92  return;
93 
94  QApplication::clipboard()->setMimeData( data, QClipboard::Clipboard );
95 }
96 
97 
98 void ClipboardController::copy()
99 {
100  QMimeData *data = mSelectionControl->copySelectedData();
101  if( !data )
102  return;
103 
104  QApplication::clipboard()->setMimeData( data, QClipboard::Clipboard );
105 }
106 
107 
108 void ClipboardController::paste()
109 {
110  const QMimeData *data = QApplication::clipboard()->mimeData( QClipboard::Clipboard );
111 
112  mMimeDataControl->insertData( data );
113 }
114 
115 void ClipboardController::onReadOnlyChanged( bool isReadOnly )
116 {
117  const QMimeData* mimeData = QApplication::clipboard()->mimeData( QClipboard::Clipboard );
118 
119  const bool hasSelectedData = ( mSelectionControl != 0 ) ? mSelectionControl->hasSelectedData() : false;
120  const bool isWriteable = !isReadOnly;
121  const bool isPastable = isWriteable && ! mimeData->formats().isEmpty() && mMimeDataControl->canReadData( mimeData );
122 
123  mCutAction->setEnabled( hasSelectedData && isWriteable );
124  mPasteAction->setEnabled( isPastable );
125 }
126 
127 void ClipboardController::onHasSelectedDataChanged( bool hasSelectedData )
128 {
129  const bool isWriteable = ( mMimeDataControl != 0 && !mModel->isReadOnly() );
130 
131  mCopyAction->setEnabled( hasSelectedData );
132  mCutAction->setEnabled( hasSelectedData && isWriteable );
133 }
134 
135 void ClipboardController::onClipboardDataChanged()
136 {
137  const QMimeData* mimeData = QApplication::clipboard()->mimeData( QClipboard::Clipboard );
138 
139  const bool isWriteable = ( mMimeDataControl != 0 && !mModel->isReadOnly() );
140  const bool isPastable = isWriteable && ! mimeData->formats().isEmpty() && mMimeDataControl->canReadData( mimeData );
141 
142  mPasteAction->setEnabled( isPastable );
143 }
144 
145 }
clipboardcontroller.h
abstractmodel.h
Kasten2::If::SelectedDataWriteable::canReadData
virtual bool canReadData(const QMimeData *data) const =0
Kasten2::If::DataSelectable::copySelectedData
virtual QMimeData * copySelectedData() 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::ClipboardController::ClipboardController
ClipboardController(KXMLGUIClient *guiClient)
Definition: clipboardcontroller.cpp:45
Kasten2::If::DataSelectable::hasSelectedData
virtual bool hasSelectedData() const =0
Kasten2::If::SelectedDataWriteable::insertData
virtual void insertData(const QMimeData *data)=0
selecteddatawriteable.h
Kasten2::If::SelectedDataWriteable::cutSelectedData
virtual QMimeData * cutSelectedData()=0
Kasten2::If::SelectedDataWriteable
Definition: selecteddatawriteable.h:43
Kasten2::If::DataSelectable
Definition: dataselectable.h:42
Kasten2::AbstractModel
Definition: abstractmodel.h:40
dataselectable.h
Kasten2::AbstractModel::isReadOnly
virtual bool isReadOnly() const
default returns true
Definition: abstractmodel.cpp:39
Kasten2::ClipboardController::setTargetModel
virtual void setTargetModel(AbstractModel *model)
Definition: clipboardcontroller.cpp:59
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:07 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