• 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
  • search
searchtool.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 "searchtool.h"
24 
25 // controller
26 #include "searchuserqueryable.h"
27 #include "searchjob.h"
28 // lib
29 #include <bytearrayview.h>
30 #include <bytearraydocument.h>
31 // Okteta core
32 #include <charcodec.h>
33 #include <abstractbytearraymodel.h>
34 // KDE
35 #include <KLocale>
36 // Qt
37 #include <QtGui/QApplication>
38 
39 
40 namespace Kasten2
41 {
42 
43 SearchTool::SearchTool()
44  : mCaseSensitivity( Qt::CaseSensitive ),
45  mUserQueryAgent( 0 ),
46  mByteArrayView( 0 ),
47  mByteArrayModel( 0 )
48 {
49  setObjectName( QLatin1String( "Search" ) );
50 }
51 
52 bool SearchTool::isApplyable() const
53 {
54  return ( mByteArrayView && mByteArrayModel );
55 // const int newPosition = finalTargetOffset();
56 
57 // return ( mByteArrayView && mByteArrayModel
58 // && (0 <= newPosition) && (newPosition <= mByteArrayModel->size()) );
59 }
60 
61 QString SearchTool::title() const { return i18nc("@title", "Search"); }
62 
63 bool SearchTool::hasSelectedData() const { return mByteArrayView->hasSelectedData(); }
64 QString SearchTool::charCodingName() const { return mByteArrayView->charCodingName(); }
65 
66 void SearchTool::setTargetModel( AbstractModel* model )
67 {
68  const bool oldIsApplyable = isApplyable();
69 
70  if( mByteArrayView ) mByteArrayView->disconnect( this );
71  if( mByteArrayModel ) mByteArrayModel->disconnect( this );
72 
73  mByteArrayView = model ? model->findBaseModel<ByteArrayView*>() : 0;
74 
75  ByteArrayDocument* document =
76  mByteArrayView ? qobject_cast<ByteArrayDocument*>( mByteArrayView->baseModel() ) : 0;
77  mByteArrayModel = document ? document->content() : 0;
78 
79  if( mByteArrayView && mByteArrayModel )
80  {
81  connect( mByteArrayView, SIGNAL(charCodecChanged(QString)),
82  SIGNAL(charCodecChanged(QString)) );
83  // TODO: update isApplyable on cursor movement and size changes
84  }
85 
86  const bool newIsApplyable = isApplyable();
87  if( oldIsApplyable != newIsApplyable )
88  emit isApplyableChanged( newIsApplyable );
89 }
90 
91 
92 void SearchTool::setUserQueryAgent( If::SearchUserQueryable* userQueryAgent )
93 {
94  mUserQueryAgent = userQueryAgent;
95 }
96 
97 void SearchTool::setSearchData( const QByteArray& searchData )
98 {
99 // const bool oldIsApplyable = isApplyable();
100 
101  mSearchData = searchData;
102 
103 // const bool newIsApplyable = isApplyable();
104 // if( oldIsApplyable != newIsApplyable )
105 // emit isApplyableChanged( newIsApplyable );
106 }
107 
108 void SearchTool::setCaseSensitivity( Qt::CaseSensitivity caseSensitivity )
109 {
110 // const bool oldIsApplyable = isApplyable();
111 
112  mCaseSensitivity = caseSensitivity;
113 
114 // const bool newIsApplyable = isApplyable();
115 // if( oldIsApplyable != newIsApplyable )
116 // emit isApplyableChanged( newIsApplyable );
117 }
118 
119 void SearchTool::search( KFindDirection direction, bool fromCursor, bool inSelection )
120 {
121  mPreviousFound = false;
122 
123  if( inSelection )
124  {
125  const Okteta::AddressRange selection = mByteArrayView->selection();
126  if( ! selection.isValid() )
127  {
128  // nothing selected, so skip any search and finish now
129  emit dataNotFound();
130  return;
131  }
132 
133  mSearchFirstIndex = selection.start();
134  mSearchLastIndex = selection.end();
135  }
136  else
137  {
138  const Okteta::Address cursorPosition = mByteArrayView->cursorPosition();
139  if( fromCursor && (cursorPosition!=0) )
140  {
141  mSearchFirstIndex = cursorPosition;
142  mSearchLastIndex = cursorPosition - 1;
143  }
144  else
145  {
146  mSearchFirstIndex = 0;
147  mSearchLastIndex = mByteArrayModel->size() - 1;
148  }
149  }
150 
151  doSearch( direction );
152 }
153 
154 void SearchTool::doSearch( KFindDirection direction )
155 {
156  // TODO: should start at last
157  Okteta::Address startIndex = (direction==FindForward) ? mSearchFirstIndex : mSearchLastIndex/*-mSearchData.size()*/;
158  bool wrapEnabled = (direction==FindForward) ? (mSearchLastIndex<startIndex) : (startIndex<mSearchFirstIndex);
159 
160  while( true )
161  {
162  QApplication::setOverrideCursor( Qt::WaitCursor );
163 
164  Okteta::Address endIndex = wrapEnabled ?
165  ( (direction==FindForward) ? mByteArrayModel->size()-1 : 0 ) :
166  ( (direction==FindForward) ? mSearchLastIndex : mSearchFirstIndex );
167 
168  SearchJob* searchJob =
169  new SearchJob( mByteArrayModel, mSearchData, startIndex, endIndex, mCaseSensitivity, mByteArrayView->charCodingName() );
170  const Okteta::Address pos = searchJob->exec();
171 
172  QApplication::restoreOverrideCursor();
173 
174  if( pos != -1 )
175  {
176  mPreviousFound = true;
177  mByteArrayView->setSelection( pos, pos+mSearchData.size()-1 );
178  break;
179  }
180 
181  if( wrapEnabled )
182  {
183  const bool wrapping = mUserQueryAgent ? mUserQueryAgent->queryContinue( direction ) : true;
184 
185  if( ! wrapping )
186  break;
187  startIndex = ( direction == FindForward ) ? 0 : mByteArrayModel->size()-1;
188  wrapEnabled = false;
189  }
190  else
191  {
192  if( !mPreviousFound )
193  emit dataNotFound();
194  break;
195  }
196  }
197  mByteArrayView->setFocus();
198 }
199 
200 
201 SearchTool::~SearchTool()
202 {
203 }
204 
205 }
Kasten2::ByteArrayView::setFocus
virtual void setFocus()
Definition: bytearrayview.cpp:155
Kasten2::SearchTool::charCodecChanged
void charCodecChanged(const QString &codecName)
Kasten2::SearchTool::setCaseSensitivity
void setCaseSensitivity(Qt::CaseSensitivity caseSensitivity)
Definition: searchtool.cpp:108
Okteta::Address
qint32 Address
Definition: address.h:34
abstractbytearraymodel.h
Kasten2::ByteArrayView::setSelection
void setSelection(Okteta::Address start, Okteta::Address end)
Definition: bytearrayview.cpp:276
searchtool.h
Kasten2::SearchTool::isApplyableChanged
void isApplyableChanged(bool isApplyable)
Kasten2::SearchJob::exec
Okteta::Address exec()
Definition: searchjob.cpp:52
Kasten2::SearchTool::searchData
QByteArray searchData() const
Definition: searchtool.h:106
Kasten2::ByteArrayView::charCodingName
QString charCodingName() const
Definition: bytearrayview.cpp:255
Kasten2::SearchTool::mSearchFirstIndex
Okteta::Address mSearchFirstIndex
Definition: searchtool.h:94
Kasten2::SearchTool::mPreviousFound
bool mPreviousFound
Definition: searchtool.h:93
KDE::NumberRange< Address, Size >
KDE::Range::start
T start() const
Definition: range.h:86
Kasten2::SearchTool::search
void search(KFindDirection direction, bool fromCursor, bool inSelection)
Definition: searchtool.cpp:119
Kasten2::SearchTool::setSearchData
void setSearchData(const QByteArray &searchData)
Definition: searchtool.cpp:97
Kasten2::If::SearchUserQueryable::queryContinue
virtual bool queryContinue(KFindDirection direction) const =0
Kasten2::FindForward
Definition: kfinddirection.h:30
Kasten2::SearchTool::SearchTool
SearchTool()
Definition: searchtool.cpp:43
Kasten2::SearchTool::mSearchData
QByteArray mSearchData
Definition: searchtool.h:89
Kasten2::ByteArrayView::hasSelectedData
virtual bool hasSelectedData() const
Definition: bytearrayview.cpp:178
Kasten2::SearchTool::mSearchLastIndex
Okteta::Address mSearchLastIndex
Definition: searchtool.h:95
Kasten2::AbstractModel::baseModel
AbstractModel * baseModel() const
Definition: abstractmodel.cpp:40
Kasten2::SearchTool::mByteArrayModel
Okteta::AbstractByteArrayModel * mByteArrayModel
Definition: searchtool.h:102
Kasten2::ByteArrayView::selection
Okteta::AddressRange selection() const
Definition: bytearrayview.cpp:271
Okteta::AbstractByteArrayModel::size
virtual Size size() const =0
Kasten2::SearchTool::setUserQueryAgent
void setUserQueryAgent(If::SearchUserQueryable *userQueryAgent)
Definition: searchtool.cpp:92
Kasten2::ByteArrayView::cursorPosition
Okteta::Address cursorPosition() const
Definition: bytearrayview.cpp:227
Kasten2::SearchTool::mCaseSensitivity
Qt::CaseSensitivity mCaseSensitivity
Definition: searchtool.h:90
Kasten2::SearchTool::dataNotFound
void dataNotFound()
KDE::Range::end
T end() const
Definition: range.h:88
Kasten2::SearchTool::setTargetModel
virtual void setTargetModel(AbstractModel *model)
Definition: searchtool.cpp:66
searchuserqueryable.h
Kasten2::SearchTool::charCodingName
QString charCodingName() const
Definition: searchtool.cpp:64
Kasten2::SearchTool::mUserQueryAgent
If::SearchUserQueryable * mUserQueryAgent
Definition: searchtool.h:98
Kasten2::SearchTool::doSearch
void doSearch(KFindDirection direction)
Definition: searchtool.cpp:154
searchjob.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::SearchTool::title
virtual QString title() const
Definition: searchtool.cpp:61
Kasten2::SearchTool::~SearchTool
virtual ~SearchTool()
Definition: searchtool.cpp:201
Kasten2::SearchTool::mByteArrayView
ByteArrayView * mByteArrayView
Definition: searchtool.h:101
Kasten2::SearchTool::isApplyable
bool isApplyable() const
Definition: searchtool.cpp:52
Kasten2::ByteArrayDocument
Definition: bytearraydocument.h:54
Kasten2::AbstractModel
Definition: abstractmodel.h:40
bytearraydocument.h
Kasten2::If::SearchUserQueryable
Definition: searchuserqueryable.h:38
Kasten2::SearchTool::hasSelectedData
bool hasSelectedData() const
Definition: searchtool.cpp:63
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
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