• 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
bookmarkscontroller.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 2007-2009,2012 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 "bookmarkscontroller.h"
24 
25 // controller
26 #include "bookmarkeditpopup.h"
27 // lib
28 #include <bytearrayview.h>
29 #include <bytearraydocument.h>
30 // Kasten core
31 #include <abstractmodel.h>
32 // Okteta gui
33 #include <offsetformat.h>
34 // Okteta core
35 #include <wordbytearrayservice.h>
36 #include <charcodec.h>
37 #include <bookmarkable.h>
38 #include <bookmarksconstiterator.h>
39 #include <bookmark.h>
40 #include <abstractbytearraymodel.h>
41 // KDE
42 #include <KXMLGUIClient>
43 #include <KLocale>
44 #include <KAction>
45 #include <KActionCollection>
46 #include <KStandardAction>
47 // Qt
48 #include <QtGui/QAction>
49 
50 
51 namespace Kasten2
52 {
53 
54 static const char BookmarkListActionListId[] = "bookmark_list";
55 
56 // TODO: Sortieren nach Offset oder Zeit
57 
58 BookmarksController::BookmarksController( KXMLGUIClient* guiClient )
59  : mGuiClient( guiClient ), mByteArrayView( 0 ), mByteArray( 0 ), mBookmarks( 0 )
60 {
61  KActionCollection* actionCollection = mGuiClient->actionCollection();
62 
63  mCreateAction = KStandardAction::addBookmark( this, SLOT(createBookmark()), actionCollection );
64 
65  mDeleteAction = actionCollection->addAction( QLatin1String("bookmark_remove"),
66  this, SLOT(deleteBookmark()) );
67  mDeleteAction->setText( i18nc("@action:inmenu","Remove Bookmark") );
68  mDeleteAction->setShortcut( Qt::CTRL + Qt::SHIFT + Qt::Key_B );
69 
70  mDeleteAllAction = actionCollection->addAction( QLatin1String("bookmark_remove_all"),
71  this, SLOT(deleteAllBookmarks()) );
72  mDeleteAllAction->setText( i18nc("@action:inmenu","Remove All Bookmarks") );
73 // mDeleteAllAction->setShortcut( Qt::CTRL + Qt::Key_G );
74 
75  mGotoNextBookmarkAction = actionCollection->addAction( QLatin1String("bookmark_next"),
76  this, SLOT(gotoNextBookmark()) );
77  mGotoNextBookmarkAction->setText( i18nc("@action:inmenu","Go to Next Bookmark") );
78  mGotoNextBookmarkAction->setShortcut( Qt::ALT + Qt::Key_Down );
79 
80  mGotoPreviousBookmarkAction = actionCollection->addAction( QLatin1String("bookmark_previous"),
81  this, SLOT(gotoPreviousBookmark()) );
82  mGotoPreviousBookmarkAction->setText( i18nc("@action:inmenu","Go to Previous Bookmark") );
83  mGotoPreviousBookmarkAction->setShortcut( Qt::ALT + Qt::Key_Up );
84 
85  mBookmarksActionGroup = new QActionGroup( this ); // TODO: do we use this only for the signal mapping?
86 // mBookmarksActionGroup->setExclusive( true );
87  connect( mBookmarksActionGroup, SIGNAL(triggered(QAction*)),
88  SLOT(onBookmarkTriggered(QAction*)) );
89 
90  setTargetModel( 0 );
91 }
92 
93 void BookmarksController::setTargetModel( AbstractModel* model )
94 {
95  if( mByteArrayView ) mByteArrayView->disconnect( this );
96  if( mByteArray ) mByteArray->disconnect( this );
97 
98  mByteArrayView = model ? model->findBaseModel<ByteArrayView*>() : 0;
99 
100  ByteArrayDocument* document =
101  mByteArrayView ? qobject_cast<ByteArrayDocument*>( mByteArrayView->baseModel() ) : 0;
102  mByteArray = document ? document->content() : 0;
103  mBookmarks = ( mByteArray && mByteArrayView ) ? qobject_cast<Okteta::Bookmarkable*>( mByteArray ) : 0;
104 
105  const bool hasViewWithBookmarks = ( mBookmarks != 0 );
106  int bookmarksCount = 0;
107  if( hasViewWithBookmarks )
108  {
109  bookmarksCount = mBookmarks->bookmarksCount();
110  connect( mByteArray, SIGNAL(bookmarksAdded(QList<Okteta::Bookmark>)),
111  SLOT(onBookmarksAdded(QList<Okteta::Bookmark>)) );
112  connect( mByteArray, SIGNAL(bookmarksRemoved(QList<Okteta::Bookmark>)),
113  SLOT(onBookmarksRemoved(QList<Okteta::Bookmark>)) );
114  connect( mByteArray, SIGNAL(bookmarksModified(QList<int>)),
115  SLOT(updateBookmarks()) );
116  connect( mByteArrayView, SIGNAL(cursorPositionChanged(Okteta::Address)),
117  SLOT(onCursorPositionChanged(Okteta::Address)) );
118  connect( mByteArrayView, SIGNAL(offsetCodingChanged(int)),
119  SLOT(updateBookmarks()) );
120  }
121 
122  updateBookmarks();
123 
124  const bool hasBookmarks = hasViewWithBookmarks && ( bookmarksCount != 0 );
125  if( hasViewWithBookmarks )
126  onCursorPositionChanged( mByteArrayView->cursorPosition() );
127  else
128  {
129  mCreateAction->setEnabled( false );
130  mDeleteAction->setEnabled( false );
131  }
132  mDeleteAllAction->setEnabled( hasBookmarks );
133  mGotoNextBookmarkAction->setEnabled( hasBookmarks );
134  mGotoPreviousBookmarkAction->setEnabled( hasBookmarks );
135 }
136 
137 void BookmarksController::updateBookmarks()
138 {
139  mGuiClient->unplugActionList( QLatin1String(BookmarkListActionListId) );
140 
141  qDeleteAll( mBookmarksActionGroup->actions() );
142 
143  if( mBookmarks == 0 )
144  return;
145 
146  const int startOffset = mByteArrayView->startOffset();
147  Okteta::OffsetFormat::print printFunction =
148  Okteta::OffsetFormat::printFunction( (Okteta::OffsetFormat::Format)mByteArrayView->offsetCoding() );
149 
150  char codedOffset[Okteta::OffsetFormat::MaxFormatWidth+1];
151 
152  static const int firstWithNumericShortCut = 1;
153  static const int lastWithNumericShortCut = 9;
154  int b = firstWithNumericShortCut;
155 
156  Okteta::BookmarksConstIterator bit = mBookmarks->createBookmarksConstIterator();
157  while( bit.hasNext() )
158  {
159  const Okteta::Bookmark& bookmark = bit.next();
160  printFunction( codedOffset, startOffset+bookmark.offset() );
161  QString title = i18nc( "@item description of bookmark", "%1: %2", QLatin1String(codedOffset),bookmark.name() );
162  if( b <= lastWithNumericShortCut )
163  {
164  title = QString::fromLatin1("&%1 %2").arg( b ).arg( title );
165  // = KStringHandler::rsqueeze( view->title(), MaxEntryLength );
166  ++b;
167  }
168  QAction *action = new QAction( title, mBookmarksActionGroup );
169 
170  action->setData( bookmark.offset() );
171  mBookmarksActionGroup->addAction( action );
172  }
173  mGuiClient->plugActionList( QLatin1String(BookmarkListActionListId),
174  mBookmarksActionGroup->actions() );
175 }
176 
177 void BookmarksController::onBookmarksAdded( const QList<Okteta::Bookmark> &bookmarks )
178 {
179 Q_UNUSED( bookmarks )
180  const int currentPosition = mByteArrayView->cursorPosition();
181  onCursorPositionChanged( currentPosition );
182 
183  const int bookmarksCount = mBookmarks->bookmarksCount();
184  const bool hasBookmarks = ( bookmarksCount != 0 );
185 
186  mDeleteAllAction->setEnabled( hasBookmarks );
187 
188  updateBookmarks();
189 }
190 
191 void BookmarksController::onBookmarksRemoved( const QList<Okteta::Bookmark> &bookmarks )
192 {
193 Q_UNUSED( bookmarks )
194  const int currentPosition = mByteArrayView->cursorPosition();
195  onCursorPositionChanged( currentPosition );
196 
197  const int bookmarksCount = mBookmarks->bookmarksCount();
198  const bool hasBookmarks = ( bookmarksCount != 0 );
199 
200  mDeleteAllAction->setEnabled( hasBookmarks );
201 
202  updateBookmarks();
203 }
204 
205 void BookmarksController::onCursorPositionChanged( Okteta::Address newPosition )
206 {
207  const int bookmarksCount = mBookmarks->bookmarksCount();
208  const bool hasBookmarks = ( bookmarksCount != 0 );
209  const bool isInsideByteArray = ( newPosition < mByteArray->size() );
210  bool isAtBookmark;
211  bool hasPrevious;
212  bool hasNext;
213  if( hasBookmarks )
214  {
215  isAtBookmark = mBookmarks->containsBookmarkFor( newPosition );
216  Okteta::BookmarksConstIterator bookmarksIterator = mBookmarks->createBookmarksConstIterator();
217  hasPrevious = bookmarksIterator.findPreviousFrom( newPosition );
218  hasNext = bookmarksIterator.findNextFrom( newPosition );
219  }
220  else
221  {
222  isAtBookmark = false;
223  hasPrevious = false;
224  hasNext = false;
225  }
226 
227  mCreateAction->setEnabled( !isAtBookmark && isInsideByteArray );
228  mDeleteAction->setEnabled( isAtBookmark );
229  mGotoNextBookmarkAction->setEnabled( hasNext );
230  mGotoPreviousBookmarkAction->setEnabled( hasPrevious );
231 }
232 
233 void BookmarksController::createBookmark()
234 {
235  const int cursorPosition = mByteArrayView->cursorPosition();
236 
237  // search for text at cursor
238  const Okteta::CharCodec* charCodec = Okteta::CharCodec::createCodec( mByteArrayView->charCodingName() );
239  const Okteta::WordByteArrayService textService( mByteArray, charCodec );
240  QString bookmarkName = textService.text( cursorPosition, cursorPosition+MaxBookmarkNameSize-1 );
241  delete charCodec;
242 
243  if( bookmarkName.isEmpty() )
244  bookmarkName = i18nc( "default name of a bookmark", "Bookmark" );// %1").arg( 0 ) ); // TODO: use counter like with new file, globally
245 
246  BookmarkEditPopup* bookmarkEditPopup = new BookmarkEditPopup( mByteArrayView->widget() );
247  QPoint popupPoint = mByteArrayView->cursorRect().topLeft();
248 // popupPoint.ry() += mSlider->height() / 2;
249  popupPoint = mByteArrayView->widget()->mapToGlobal( popupPoint );
250 
251  bookmarkEditPopup->setPosition( popupPoint );
252  bookmarkEditPopup->setName( bookmarkName );
253  const bool success = ( bookmarkEditPopup->exec() != 0 );
254 
255  if( success )
256  {
257  Okteta::Bookmark bookmark( cursorPosition );
258  bookmark.setName( bookmarkEditPopup->name() );
259 
260  QList<Okteta::Bookmark> bookmarks;
261  bookmarks.append( bookmark );
262  mBookmarks->addBookmarks( bookmarks );
263  }
264  delete bookmarkEditPopup;
265 }
266 
267 void BookmarksController::deleteBookmark()
268 {
269  const int cursorPosition = mByteArrayView->cursorPosition();
270  QList<Okteta::Bookmark> bookmarks;
271  bookmarks.append( cursorPosition );
272  mBookmarks->removeBookmarks( bookmarks );
273 }
274 
275 void BookmarksController::deleteAllBookmarks()
276 {
277  mBookmarks->removeAllBookmarks();
278 }
279 
280 void BookmarksController::gotoNextBookmark()
281 {
282  const int currentPosition = mByteArrayView->cursorPosition();
283 
284  Okteta::BookmarksConstIterator bookmarksIterator = mBookmarks->createBookmarksConstIterator();
285  const bool hasNext = bookmarksIterator.findNextFrom( currentPosition );
286  if( hasNext )
287  {
288  const int newPosition = bookmarksIterator.next().offset();
289  mByteArrayView->setCursorPosition( newPosition );
290  }
291 }
292 
293 void BookmarksController::gotoPreviousBookmark()
294 {
295  const int currentPosition = mByteArrayView->cursorPosition();
296 
297  Okteta::BookmarksConstIterator bookmarksIterator = mBookmarks->createBookmarksConstIterator();
298  const bool hasPrevious = bookmarksIterator.findPreviousFrom( currentPosition );
299  if( hasPrevious )
300  {
301  const int newPosition = bookmarksIterator.previous().offset();
302  mByteArrayView->setCursorPosition( newPosition );
303  }
304 }
305 
306 void BookmarksController::onBookmarkTriggered( QAction* action )
307 {
308  const int newPosition = action->data().toInt();
309  mByteArrayView->setCursorPosition( newPosition );
310 }
311 
312 
313 BookmarksController::~BookmarksController() {}
314 
315 }
abstractmodel.h
wordbytearrayservice.h
Okteta::Address
qint32 Address
Definition: address.h:34
abstractbytearraymodel.h
bookmark.h
Kasten2::ByteArrayView::widget
virtual QWidget * widget() const
Definition: bytearrayview.cpp:147
Okteta::Bookmark::offset
Address offset() const
Definition: bookmark.h:66
Kasten2::ByteArrayView::charCodingName
QString charCodingName() const
Definition: bytearrayview.cpp:255
Okteta::Bookmark
Definition: bookmark.h:38
Kasten2::BookmarksController::BookmarksController
BookmarksController(KXMLGUIClient *guiClient)
Definition: bookmarkscontroller.cpp:58
Okteta::Bookmark::name
QString name() const
Definition: bookmark.h:67
Okteta::OffsetFormat::print
void(* print)(char *Buffer, unsigned int Offset)
Definition: offsetformat.h:41
Okteta::OffsetFormat::Format
Format
Definition: offsetformat.h:43
Okteta::Bookmarkable::addBookmarks
virtual void addBookmarks(const QList< Okteta::Bookmark > &bookmarks)=0
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::BookmarksConstIterator::findPreviousFrom
bool findPreviousFrom(unsigned int offset)
Definition: bookmarksconstiterator.h:77
Okteta::Bookmarkable::removeBookmarks
virtual void removeBookmarks(const QList< Okteta::Bookmark > &bookmarks)=0
Okteta::OffsetFormat::MaxFormatWidth
static const int MaxFormatWidth
Definition: offsetformat.h:45
Okteta::AbstractByteArrayModel::size
virtual Size size() const =0
Okteta::Bookmarkable::removeAllBookmarks
virtual void removeAllBookmarks()=0
Kasten2::ByteArrayView::cursorPosition
Okteta::Address cursorPosition() const
Definition: bytearrayview.cpp:227
Okteta::OffsetFormat::printFunction
static print printFunction(int i)
Definition: offsetformat.h:73
Okteta::CharCodec
Definition: charcodec.h:42
Okteta::BookmarksConstIterator::previous
const Okteta::Bookmark & previous()
Definition: bookmarksconstiterator.h:79
bookmarksconstiterator.h
bookmarkscontroller.h
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
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
Okteta::Bookmarkable::bookmarksCount
virtual unsigned int bookmarksCount() const =0
Kasten2::BookmarksController::setTargetModel
virtual void setTargetModel(AbstractModel *model)
Definition: bookmarkscontroller.cpp:93
offsetformat.h
Kasten2::ByteArrayDocument
Definition: bytearraydocument.h:54
Kasten2::AbstractModel
Definition: abstractmodel.h:40
Kasten2::ByteArrayView::offsetCoding
int offsetCoding() const
Definition: bytearrayview.cpp:296
bytearraydocument.h
Kasten2::ByteArrayView::cursorRect
QRect cursorRect() const
Definition: bytearrayview.cpp:231
Okteta::BookmarksConstIterator::findNextFrom
bool findNextFrom(unsigned int offset)
Definition: bookmarksconstiterator.h:76
Kasten2::ByteArrayView::startOffset
Okteta::Address startOffset() const
Definition: bytearrayview.cpp:236
Okteta::Bookmark::setName
void setName(const QString &name)
Definition: bookmark.h:70
bookmarkeditpopup.h
Kasten2::ByteArrayView
Definition: bytearrayview.h:51
Kasten2::BookmarksController::~BookmarksController
virtual ~BookmarksController()
Definition: bookmarkscontroller.cpp:313
Okteta::Bookmarkable::containsBookmarkFor
virtual bool containsBookmarkFor(int offset) const =0
bookmarkable.h
Kasten2::BookmarkListActionListId
static const char BookmarkListActionListId[]
Definition: bookmarkscontroller.cpp:54
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