• 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
  • bookmarks
bookmarkstool.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 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 "bookmarkstool.h"
24 
25 // Kasten core
26 #include <bytearrayview.h>
27 #include <bytearraydocument.h>
28 // Kasten core
29 #include <abstractmodel.h>
30 // Okteta gui
31 #include <offsetformat.h>
32 // Okteta core
33 #include <wordbytearrayservice.h>
34 #include <charcodec.h>
35 #include <bookmarkable.h>
36 #include <bookmarksconstiterator.h>
37 #include <bookmark.h>
38 #include <abstractbytearraymodel.h>
39 // KDE
40 #include <KLocale>
41 // Qt
42 #include <QtGui/QWidget>
43 
44 
45 namespace Kasten2
46 {
47 
48 BookmarksTool::BookmarksTool()
49  : mByteArrayView( 0 ), mByteArray( 0 ), mBookmarks( 0 ), mCanCreateBookmark( false )
50 {
51  setObjectName( QLatin1String( "Bookmarks" ) );
52 }
53 
54 
55 QString BookmarksTool::title() const { return i18nc("@title:window", "Bookmarks"); }
56 bool BookmarksTool::canCreateBookmark() const { return mCanCreateBookmark; }
57 const Okteta::Bookmark& BookmarksTool::bookmarkAt( unsigned int index ) const { return mBookmarks->bookmarkAt( index ); }
58 int BookmarksTool::indexOf( const Okteta::Bookmark& bookmark ) const
59 {
60  int result = -1;
61 
62  Okteta::BookmarksConstIterator bit = mBookmarks->createBookmarksConstIterator();
63  int i = 0;
64  while( bit.hasNext() )
65  {
66  if( bookmark == bit.next() )
67  {
68  result = i;
69  break;
70  }
71  ++i;
72  }
73  return result;
74 }
75 unsigned int BookmarksTool::bookmarksCount() const { return mBookmarks ? mBookmarks->bookmarksCount() : 0; }
76 int BookmarksTool::offsetCoding() const { return mByteArrayView ? mByteArrayView->offsetCoding() : 0; }
77 
78 
79 void BookmarksTool::setTargetModel( AbstractModel* model )
80 {
81  const int oldOffsetCoding = offsetCoding();
82 
83  if( mByteArrayView ) mByteArrayView->disconnect( this );
84  if( mByteArray ) mByteArray->disconnect( this );
85 
86  mByteArrayView = model ? model->findBaseModel<ByteArrayView*>() : 0;
87 
88  ByteArrayDocument* document =
89  mByteArrayView ? qobject_cast<ByteArrayDocument*>( mByteArrayView->baseModel() ) : 0;
90  mByteArray = document ? document->content() : 0;
91  mBookmarks = ( mByteArray && mByteArrayView ) ? qobject_cast<Okteta::Bookmarkable*>( mByteArray ) : 0;
92 
93  const bool hasViewWithBookmarks = ( mBookmarks != 0 );
94  if( hasViewWithBookmarks )
95  {
96  onCursorPositionChanged( mByteArrayView->cursorPosition() );
97 
98  connect( mByteArray, SIGNAL(bookmarksAdded(QList<Okteta::Bookmark>)),
99  SIGNAL(bookmarksAdded(QList<Okteta::Bookmark>)) );
100  connect( mByteArray, SIGNAL(bookmarksRemoved(QList<Okteta::Bookmark>)),
101  SIGNAL(bookmarksRemoved(QList<Okteta::Bookmark>)) );
102  connect( mByteArray, SIGNAL(bookmarksAdded(QList<Okteta::Bookmark>)),
103  SLOT(onBookmarksModified()) );
104  connect( mByteArray, SIGNAL(bookmarksRemoved(QList<Okteta::Bookmark>)),
105  SLOT(onBookmarksModified()) );
106  connect( mByteArray, SIGNAL(bookmarksModified(QList<int>)),
107  SIGNAL(bookmarksModified(QList<int>)) );
108  connect( mByteArrayView, SIGNAL(cursorPositionChanged(Okteta::Address)),
109  SLOT(onCursorPositionChanged(Okteta::Address)) );
110  connect( mByteArrayView, SIGNAL(offsetCodingChanged(int)),
111  SIGNAL(offsetCodingChanged(int)) );
112  }
113  else
114  {
115  static const bool cantCreateBookmark = false;
116  if( mCanCreateBookmark != cantCreateBookmark )
117  {
118  mCanCreateBookmark = cantCreateBookmark;
119  emit canCreateBookmarkChanged( cantCreateBookmark );
120  }
121  }
122 
123  const int newOffsetCoding = offsetCoding();
124  if( oldOffsetCoding != newOffsetCoding )
125  emit offsetCodingChanged( newOffsetCoding );
126  emit hasBookmarksChanged( hasViewWithBookmarks );
127 }
128 
129 Okteta::Bookmark BookmarksTool::createBookmark()
130 {
131  Okteta::Bookmark bookmark;
132 
133  if( mBookmarks )
134  {
135  const int cursorPosition = mByteArrayView->cursorPosition();
136 
137  // search for text at cursor
138  const Okteta::CharCodec* charCodec = Okteta::CharCodec::createCodec( mByteArrayView->charCodingName() );
139  const Okteta::WordByteArrayService textService( mByteArray, charCodec );
140  QString bookmarkName = textService.text( cursorPosition, cursorPosition+MaxBookmarkNameSize-1 );
141  delete charCodec;
142 
143  if( bookmarkName.isEmpty() )
144  bookmarkName = i18nc( "default name of a bookmark", "Bookmark" );
145  // %1").arg( 0 ) ); // TODO: use counter like with new file, globally
146 
147  bookmark.setOffset( mByteArrayView->cursorPosition() );
148  bookmark.setName( bookmarkName );
149 
150  QList<Okteta::Bookmark> bookmarksToBeCreated;
151  bookmarksToBeCreated.append( bookmark );
152  mBookmarks->addBookmarks( bookmarksToBeCreated );
153  }
154 
155  return bookmark;
156 }
157 
158 void BookmarksTool::deleteBookmarks( const QList<Okteta::Bookmark>& bookmarks )
159 {
160  if( mBookmarks )
161  mBookmarks->removeBookmarks( bookmarks );
162  mByteArrayView->widget()->setFocus();
163 }
164 
165 void BookmarksTool::gotoBookmark( const Okteta::Bookmark& bookmark )
166 {
167  if( mByteArrayView )
168  {
169  mByteArrayView->setCursorPosition( bookmark.offset() );
170  mByteArrayView->widget()->setFocus();
171  }
172 }
173 
174 void BookmarksTool::setBookmarkName( unsigned int bookmarkIndex, const QString& name )
175 {
176  Okteta::Bookmark bookmark = mBookmarks->bookmarkAt( bookmarkIndex );
177 
178  bookmark.setName( name );
179  mBookmarks->setBookmark( bookmarkIndex, bookmark );
180 
181  mByteArrayView->widget()->setFocus();
182 }
183 
184 void BookmarksTool::onCursorPositionChanged( Okteta::Address newPosition )
185 {
186  const int bookmarksCount = mBookmarks->bookmarksCount();
187  const bool hasBookmarks = ( bookmarksCount != 0 );
188  const bool isInsideByteArray = ( newPosition < mByteArray->size() );
189  const bool isAtBookmark = hasBookmarks ? mBookmarks->containsBookmarkFor( newPosition ) : false;
190  const bool canCreateBookmark = ( !isAtBookmark && isInsideByteArray );
191 
192  if( canCreateBookmark != mCanCreateBookmark )
193  {
194  mCanCreateBookmark = canCreateBookmark;
195  emit canCreateBookmarkChanged( canCreateBookmark );
196  }
197 }
198 
199 // TODO: is a hack
200 // better just only check for the added and removed, if they include the current position, then change mCanCreateBookmark
201 void BookmarksTool::onBookmarksModified()
202 {
203  const int cursorPosition = mByteArrayView->cursorPosition();
204  onCursorPositionChanged( cursorPosition );
205 }
206 
207 BookmarksTool::~BookmarksTool() {}
208 
209 }
abstractmodel.h
wordbytearrayservice.h
Okteta::Address
qint32 Address
Definition: address.h:34
Kasten2::BookmarksTool::gotoBookmark
void gotoBookmark(const Okteta::Bookmark &bookmark)
Definition: bookmarkstool.cpp:165
abstractbytearraymodel.h
bookmark.h
Kasten2::BookmarksTool::canCreateBookmark
bool canCreateBookmark() const
Definition: bookmarkstool.cpp:56
Kasten2::ByteArrayView::widget
virtual QWidget * widget() const
Definition: bytearrayview.cpp:147
Okteta::Bookmark::offset
Address offset() const
Definition: bookmark.h:66
Kasten2::BookmarksTool::hasBookmarksChanged
void hasBookmarksChanged(bool hasBookmarks)
Kasten2::ByteArrayView::charCodingName
QString charCodingName() const
Definition: bytearrayview.cpp:255
Okteta::Bookmark
Definition: bookmark.h:38
Kasten2::BookmarksTool::createBookmark
Okteta::Bookmark createBookmark()
Definition: bookmarkstool.cpp:129
Kasten2::BookmarksTool::hasBookmarks
bool hasBookmarks() const
Okteta::Bookmark::setOffset
void setOffset(Address offset)
Definition: bookmark.h:71
Kasten2::BookmarksTool::title
virtual QString title() const
Definition: bookmarkstool.cpp:55
Kasten2::BookmarksTool::indexOf
int indexOf(const Okteta::Bookmark &bookmark) const
Definition: bookmarkstool.cpp:58
Okteta::Bookmarkable::addBookmarks
virtual void addBookmarks(const QList< Okteta::Bookmark > &bookmarks)=0
Kasten2::BookmarksTool::bookmarkAt
const Okteta::Bookmark & bookmarkAt(unsigned int index) const
Definition: bookmarkstool.cpp:57
Kasten2::BookmarksTool::bookmarksRemoved
void bookmarksRemoved(const QList< Okteta::Bookmark > &bookmarks)
Kasten2::BookmarksTool::setTargetModel
virtual void setTargetModel(AbstractModel *model)
Definition: bookmarkstool.cpp:79
Kasten2::AbstractModel::baseModel
AbstractModel * baseModel() const
Definition: abstractmodel.cpp:40
Okteta::Bookmarkable::createBookmarksConstIterator
virtual Okteta::BookmarksConstIterator createBookmarksConstIterator() const =0
Okteta::BookmarksConstIterator
Definition: bookmarksconstiterator.h:36
Okteta::Bookmarkable::removeBookmarks
virtual void removeBookmarks(const QList< Okteta::Bookmark > &bookmarks)=0
Okteta::AbstractByteArrayModel::size
virtual Size size() const =0
Kasten2::ByteArrayView::cursorPosition
Okteta::Address cursorPosition() const
Definition: bytearrayview.cpp:227
Okteta::CharCodec
Definition: charcodec.h:42
bookmarksconstiterator.h
Kasten2::BookmarksTool::~BookmarksTool
virtual ~BookmarksTool()
Definition: bookmarkstool.cpp:207
Okteta::CharCodec::createCodec
static CharCodec * createCodec(CharCoding charCoding)
Definition: charcodec.cpp:68
Kasten2::ByteArrayView::setCursorPosition
void setCursorPosition(Okteta::Address cursorPosition)
Definition: bytearrayview.cpp:217
Okteta::WordByteArrayService
Definition: wordbytearrayservice.h:44
Kasten2::BookmarksTool::canCreateBookmarkChanged
void canCreateBookmarkChanged(bool canCreateBookmark)
Okteta::Bookmarkable::bookmarkAt
virtual const Okteta::Bookmark & bookmarkAt(unsigned int index) const =0
Kasten2::BookmarksTool::BookmarksTool
BookmarksTool()
Definition: bookmarkstool.cpp:48
Okteta::BookmarksConstIterator::next
const Okteta::Bookmark & next()
Definition: bookmarksconstiterator.h:78
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
charcodec.h
Okteta::BookmarksConstIterator::hasNext
bool hasNext() const
Definition: bookmarksconstiterator.h:69
Kasten2::BookmarksTool::bookmarksModified
void bookmarksModified(const QList< int > &indizes)
Kasten2::BookmarksTool::deleteBookmarks
void deleteBookmarks(const QList< Okteta::Bookmark > &bookmarks)
Definition: bookmarkstool.cpp:158
Okteta::Bookmarkable::bookmarksCount
virtual unsigned int bookmarksCount() const =0
Kasten2::BookmarksTool::offsetCoding
int offsetCoding() const
Definition: bookmarkstool.cpp:76
Kasten2::BookmarksTool::bookmarksCount
unsigned int bookmarksCount() const
Definition: bookmarkstool.cpp:75
bookmarkstool.h
Kasten2::BookmarksTool::offsetCodingChanged
void offsetCodingChanged(int offsetCoding)
offsetformat.h
Okteta::Bookmarkable::setBookmark
virtual void setBookmark(unsigned int index, const Okteta::Bookmark &bookmark)=0
Kasten2::ByteArrayDocument
Definition: bytearraydocument.h:54
Kasten2::AbstractModel
Definition: abstractmodel.h:40
Kasten2::ByteArrayView::offsetCoding
int offsetCoding() const
Definition: bytearrayview.cpp:296
bytearraydocument.h
Okteta::Bookmark::setName
void setName(const QString &name)
Definition: bookmark.h:70
Kasten2::BookmarksTool::setBookmarkName
void setBookmarkName(unsigned int bookmarkIndex, const QString &name)
Definition: bookmarkstool.cpp:174
Kasten2::ByteArrayView
Definition: bytearrayview.h:51
Okteta::Bookmarkable::containsBookmarkFor
virtual bool containsBookmarkFor(int offset) const =0
Kasten2::BookmarksTool::bookmarksAdded
void bookmarksAdded(const QList< Okteta::Bookmark > &bookmarks)
bookmarkable.h
QList
Definition: bookmarkable.h:29
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: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