• 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
  • kasten
  • controllers
  • view
  • replace
replacetool.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the Okteta Kasten module, made within the KDE community.
3 
4  Copyright 2009 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 "replacetool.h"
24 
25 // controller
26 #include "replaceuserqueryable.h"
27 // search controller
28 #include "../search/searchjob.h"
29 // lib
30 #include <bytearrayview.h>
31 #include <bytearraydocument.h>
32 // Okteta core
33 #include <charcodec.h>
34 #include <abstractbytearraymodel.h>
35 // KDE
36 #include <KLocale>
37 // Qt
38 #include <QtGui/QApplication>
39 
40 
41 namespace Kasten2
42 {
43 
44 ReplaceTool::ReplaceTool()
45  : mCaseSensitivity( Qt::CaseSensitive ),
46  mUserQueryAgent( 0 ),
47  mByteArrayView( 0 ),
48  mByteArrayModel( 0 )
49 {
50  setObjectName( QLatin1String( "Replace" ) );
51 }
52 
53 bool ReplaceTool::isApplyable() const
54 {
55  return ( mByteArrayView && mByteArrayModel && !mByteArrayView->isReadOnly() );
56 // const int newPosition = finalTargetOffset();
57 
58 // return ( mByteArrayView && mByteArrayModel
59 // && (0 <= newPosition) && (newPosition <= mByteArrayModel->size()) );
60 }
61 
62 QString ReplaceTool::title() const { return i18nc("@title", "Replace"); }
63 
64 bool ReplaceTool::hasSelectedData() const { return mByteArrayView->hasSelectedData(); }
65 QString ReplaceTool::charCodingName() const { return mByteArrayView->charCodingName(); }
66 
67 
68 void ReplaceTool::setTargetModel( AbstractModel* model )
69 {
70  const bool oldIsApplyable = isApplyable();
71 
72  if( mByteArrayView ) mByteArrayView->disconnect( this );
73  if( mByteArrayModel ) mByteArrayModel->disconnect( this );
74 
75  mByteArrayView = model ? model->findBaseModel<ByteArrayView*>() : 0;
76 
77  ByteArrayDocument* document =
78  mByteArrayView ? qobject_cast<ByteArrayDocument*>( mByteArrayView->baseModel() ) : 0;
79  mByteArrayModel = document ? document->content() : 0;
80 
81  if( mByteArrayView && mByteArrayModel )
82  {
83  connect( mByteArrayView, SIGNAL(readOnlyChanged(bool)), SLOT(onReadOnlyChanged(bool)) );
84  // TODO: update isApplyable on cursor movement and size changes
85  }
86 
87  const bool newIsApplyable = isApplyable();
88  if( oldIsApplyable != newIsApplyable )
89  emit isApplyableChanged( newIsApplyable );
90 }
91 
92 
93 void ReplaceTool::setUserQueryAgent( If::ReplaceUserQueryable* userQueryAgent )
94 {
95  mUserQueryAgent = userQueryAgent;
96 }
97 
98 void ReplaceTool::setSearchData( const QByteArray& searchData )
99 {
100 // const bool oldIsApplyable = isApplyable();
101 
102  mSearchData = searchData;
103 
104 // const bool newIsApplyable = isApplyable();
105 // if( oldIsApplyable != newIsApplyable )
106 // emit isApplyableChanged( newIsApplyable );
107 }
108 
109 void ReplaceTool::setReplaceData( const QByteArray& replaceData )
110 {
111 // const bool oldIsApplyable = isApplyable();
112 
113  mReplaceData = replaceData;
114 
115 // const bool newIsApplyable = isApplyable();
116 // if( oldIsApplyable != newIsApplyable )
117 // emit isApplyableChanged( newIsApplyable );
118 }
119 
120 void ReplaceTool::setCaseSensitivity( Qt::CaseSensitivity caseSensitivity )
121 {
122 // const bool oldIsApplyable = isApplyable();
123 
124  mCaseSensitivity = caseSensitivity;
125 
126 // const bool newIsApplyable = isApplyable();
127 // if( oldIsApplyable != newIsApplyable )
128 // emit isApplyableChanged( newIsApplyable );
129 }
130 
131 void ReplaceTool::setDoPrompt( int doPrompt )
132 {
133  mDoPrompt = doPrompt;
134 }
135 
136 void ReplaceTool::replace( KFindDirection direction, bool fromCursor, bool inSelection )
137 {
138  mPreviousFound = false;
139 
140  Okteta::Address startIndex;
141 
142  if( inSelection )
143  {
144  const Okteta::AddressRange selection = mByteArrayView->selection();
145  if( ! selection.isValid() )
146  {
147  // nothing selected, so skip any search and finish now
148  emit finished( false, 0 );
149  return;
150  }
151 
152  mReplaceFirstIndex = selection.start();
153  mReplaceLastIndex = selection.end();
154  startIndex = selection.start();
155  mDoWrap = true; // TODO: no wrapping needed, or?
156  direction = FindForward; // TODO: why only forward?
157  }
158  else
159  {
160  const Okteta::Address cursorPosition = mByteArrayView->cursorPosition();
161  if( fromCursor && (cursorPosition!=0) )
162  {
163  mReplaceFirstIndex = cursorPosition;
164  mReplaceLastIndex = cursorPosition-1;
165  }
166  else
167  {
168  mReplaceFirstIndex = 0;
169  mReplaceLastIndex = mByteArrayModel->size()-1;
170  }
171  startIndex = (direction==FindForward) ? mReplaceFirstIndex : mReplaceLastIndex/*-mSearchData.size()*/;
172  mDoWrap = (direction==FindForward) ? (mReplaceLastIndex<startIndex) : (startIndex<mReplaceFirstIndex);
173  }
174 
175  doReplace( direction, startIndex );
176 }
177 
178 void ReplaceTool::doReplace( KFindDirection direction, Okteta::Address startIndex )
179 {
180  QApplication::setOverrideCursor( Qt::WaitCursor );
181 
182  int noOfReplacements = 0;
183 
184  while( true )
185  {
186  const bool isForward = ( direction == FindForward );
187  Okteta::Address endIndex = mDoWrap ?
188  ( isForward ? mByteArrayModel->size()-1 : 0 ) :
189  ( isForward ? mReplaceLastIndex : mReplaceFirstIndex );
190 
191  SearchJob* searchJob =
192  new SearchJob( mByteArrayModel, mSearchData, startIndex, endIndex, mCaseSensitivity, mByteArrayView->charCodingName() );
193  const Okteta::Address pos = searchJob->exec();
194 
195  if( pos != -1 )
196  {
197  mPreviousFound = true;
198 
199  startIndex = pos;
200  bool currentToBeReplaced;
201  bool isCancelled;
202 
203  if( mDoPrompt )
204  {
205  QApplication::restoreOverrideCursor();
206 
207  mByteArrayView->setSelection( pos, pos+mSearchData.size()-1 );
208 
209  const ReplaceBehaviour replaceBehaviour = mUserQueryAgent ?
210  mUserQueryAgent->queryReplaceCurrent() :
211  ReplaceAll;
212 
213  mByteArrayView->selectAllData( false );
214 
215  switch( replaceBehaviour )
216  {
217  case ReplaceAll:
218  mDoPrompt = false;
219  currentToBeReplaced = true;
220  isCancelled = false;
221  break;
222  case ReplaceCurrent:
223  currentToBeReplaced = true;
224  isCancelled = false;
225  break;
226  case SkipCurrent:
227  if( isForward )
228  ++startIndex;
229  else
230  --startIndex;
231  currentToBeReplaced = false;
232  isCancelled = false;
233  break;
234  case CancelReplacing:
235  default:
236  currentToBeReplaced = false;
237  isCancelled = true;
238  mDoWrap = false;
239  }
240  }
241  else
242  {
243  currentToBeReplaced = true;
244  isCancelled = false;
245  }
246 
247  if( currentToBeReplaced )
248  {
249  ++noOfReplacements;
250  const Okteta::Size inserted = mByteArrayModel->replace( startIndex, mSearchData.size(),
251  reinterpret_cast<const Okteta::Byte*>(mReplaceData.constData()),
252  mReplaceData.size() );
253  if( isForward )
254  startIndex += inserted;
255  else
256  startIndex -= inserted;
257  }
258 
259  if( ! isCancelled )
260  continue;
261  }
262 
263  QApplication::restoreOverrideCursor();
264  // reached end
265  if( mDoWrap )
266  {
267  const bool wrapping = mUserQueryAgent ? mUserQueryAgent->queryContinue( direction, noOfReplacements ) : true;
268 
269  if( ! wrapping )
270  break;
271 
272  startIndex = (direction==FindForward) ? 0 : mByteArrayModel->size()-1;
273  mDoWrap = false;
274  noOfReplacements = 0;
275 
276  QApplication::setOverrideCursor( Qt::WaitCursor );
277  }
278  else
279  {
280  emit finished( mPreviousFound, noOfReplacements );
281 
282  break;
283  }
284  }
285 }
286 
287 void ReplaceTool::onReadOnlyChanged( bool isReadOnly )
288 {
289 Q_UNUSED( isReadOnly )
290 
291  // TODO: find out if isApplyable really changed, perhaps by caching the readonly state?
292  emit isApplyableChanged( isApplyable() );
293 }
294 
295 ReplaceTool::~ReplaceTool()
296 {
297 }
298 
299 }
Kasten2::ReplaceTool::setTargetModel
virtual void setTargetModel(AbstractModel *model)
Definition: replacetool.cpp:68
Okteta::AbstractByteArrayModel::replace
virtual Size replace(const AddressRange &removeRange, const Byte *insertData, int insertLength)=0
replaces as much as possible
Okteta::Address
qint32 Address
Definition: address.h:34
Kasten2::ReplaceTool::mUserQueryAgent
If::ReplaceUserQueryable * mUserQueryAgent
Definition: replacetool.h:106
abstractbytearraymodel.h
Kasten2::ByteArrayView::setSelection
void setSelection(Okteta::Address start, Okteta::Address end)
Definition: bytearrayview.cpp:276
Kasten2::ReplaceTool::searchData
QByteArray searchData() const
Definition: replacetool.h:114
Kasten2::ReplaceAll
Definition: replaceuserqueryable.h:35
Kasten2::ReplaceTool::replace
void replace(KFindDirection direction, bool fromCursor, bool inSelection)
Definition: replacetool.cpp:136
Kasten2::ReplaceTool::isApplyableChanged
void isApplyableChanged(bool isApplyable)
Kasten2::SearchJob::exec
Okteta::Address exec()
Definition: searchjob.cpp:52
Kasten2::ReplaceTool::mReplaceData
QByteArray mReplaceData
Definition: replacetool.h:95
Kasten2::ReplaceTool::mCaseSensitivity
Qt::CaseSensitivity mCaseSensitivity
Definition: replacetool.h:96
Kasten2::ReplaceTool::mByteArrayModel
Okteta::AbstractByteArrayModel * mByteArrayModel
Definition: replacetool.h:110
Kasten2::ReplaceTool::finished
void finished(bool previousFound, int noOfReplacements)
Kasten2::If::ReplaceUserQueryable
Definition: replaceuserqueryable.h:41
Kasten2::ByteArrayView::charCodingName
QString charCodingName() const
Definition: bytearrayview.cpp:255
KDE::NumberRange< Address, Size >
KDE::Range::start
T start() const
Definition: range.h:86
Kasten2::ReplaceTool::mPreviousFound
bool mPreviousFound
Definition: replacetool.h:100
Kasten2::ReplaceTool::mReplaceLastIndex
Okteta::Address mReplaceLastIndex
Definition: replacetool.h:103
Okteta::Byte
unsigned char Byte
Definition: byte.h:29
Kasten2::ReplaceTool::doReplace
void doReplace(KFindDirection direction, Okteta::Address startIndex)
Definition: replacetool.cpp:178
Kasten2::ByteArrayView::selectAllData
virtual void selectAllData(bool selectAll)
Definition: bytearrayview.cpp:173
Kasten2::SkipCurrent
Definition: replaceuserqueryable.h:35
Kasten2::FindForward
Definition: kfinddirection.h:30
Kasten2::ReplaceTool::title
virtual QString title() const
Definition: replacetool.cpp:62
Kasten2::ByteArrayView::hasSelectedData
virtual bool hasSelectedData() const
Definition: bytearrayview.cpp:178
Kasten2::AbstractModel::baseModel
AbstractModel * baseModel() const
Definition: abstractmodel.cpp:40
Kasten2::ByteArrayView::selection
Okteta::AddressRange selection() const
Definition: bytearrayview.cpp:271
Okteta::AbstractByteArrayModel::size
virtual Size size() const =0
Kasten2::ByteArrayView::cursorPosition
Okteta::Address cursorPosition() const
Definition: bytearrayview.cpp:227
Kasten2::ReplaceTool::ReplaceTool
ReplaceTool()
Definition: replacetool.cpp:44
Kasten2::ReplaceCurrent
Definition: replaceuserqueryable.h:35
KDE::Range::end
T end() const
Definition: range.h:88
Kasten2::ReplaceTool::setReplaceData
void setReplaceData(const QByteArray &replaceData)
Definition: replacetool.cpp:109
Kasten2::ReplaceTool::mByteArrayView
ByteArrayView * mByteArrayView
Definition: replacetool.h:109
Kasten2::ReplaceTool::mSearchData
QByteArray mSearchData
Definition: replacetool.h:94
Kasten2::ReplaceTool::hasSelectedData
bool hasSelectedData() const
Definition: replacetool.cpp:64
Kasten2::ReplaceTool::setSearchData
void setSearchData(const QByteArray &searchData)
Definition: replacetool.cpp:98
Kasten2::If::ReplaceUserQueryable::queryReplaceCurrent
virtual ReplaceBehaviour queryReplaceCurrent() const =0
replacetool.h
Kasten2::AbstractModel::findBaseModel
T findBaseModel() const
returns the first baseModel which is of type T, or null if none is found.
Definition: abstractmodel.h:93
Kasten2::SearchJob
Definition: searchjob.h:41
Kasten2::KFindDirection
KFindDirection
Definition: kfinddirection.h:29
charcodec.h
Kasten2::ReplaceTool::setUserQueryAgent
void setUserQueryAgent(If::ReplaceUserQueryable *userQueryAgent)
Definition: replacetool.cpp:93
Kasten2::ReplaceTool::setCaseSensitivity
void setCaseSensitivity(Qt::CaseSensitivity caseSensitivity)
Definition: replacetool.cpp:120
Kasten2::ReplaceTool::onReadOnlyChanged
void onReadOnlyChanged(bool isReadOnly)
Definition: replacetool.cpp:287
Kasten2::ReplaceTool::charCodingName
QString charCodingName() const
Definition: replacetool.cpp:65
Kasten2::ReplaceTool::mReplaceFirstIndex
Okteta::Address mReplaceFirstIndex
Definition: replacetool.h:102
Kasten2::ReplaceTool::~ReplaceTool
virtual ~ReplaceTool()
Definition: replacetool.cpp:295
Kasten2::ReplaceTool::mDoWrap
bool mDoWrap
Definition: replacetool.h:101
Kasten2::ByteArrayDocument
Definition: bytearraydocument.h:54
replaceuserqueryable.h
Okteta::Size
qint32 Size
Definition: size.h:33
Kasten2::AbstractModel
Definition: abstractmodel.h:40
Kasten2::If::ReplaceUserQueryable::queryContinue
virtual bool queryContinue(KFindDirection direction, int noOfReplacements) const =0
bytearraydocument.h
KDE::Range::isValid
bool isValid() const
returns true if the range covers at least one index
Definition: range.h:122
Kasten2::ByteArrayView
Definition: bytearrayview.h:51
Kasten2::ReplaceTool::mDoPrompt
bool mDoPrompt
Definition: replacetool.h:97
Kasten2::ReplaceTool::setDoPrompt
void setDoPrompt(int doPrompt)
Definition: replacetool.cpp:131
Kasten2::ReplaceBehaviour
ReplaceBehaviour
Definition: replaceuserqueryable.h:35
Kasten2::ReplaceTool::replaceData
QByteArray replaceData() const
Definition: replacetool.h:115
Kasten2::ReplaceTool::isApplyable
bool isApplyable() const
Definition: replacetool.cpp:53
Kasten2::ByteArrayView::isReadOnly
virtual bool isReadOnly() const
default returns true
Definition: bytearrayview.cpp:152
Kasten2::CancelReplacing
Definition: replaceuserqueryable.h:35
bytearrayview.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