• 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
  • viewstatus
viewstatuscontroller.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 2008-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 "viewstatuscontroller.h"
24 
25 // lib
26 #include <bytearrayview.h>
27 // Kasten ui
28 #include <togglebutton.h>
29 #include <statusbar.h>
30 // Okteta core
31 #include <charcodec.h>
32 #include <oktetacore.h>
33 // KDE
34 #include <KSqueezedTextLabel>
35 #include <KComboBox>
36 #include <KLocale>
37 // Qt
38 #include <QtGui/QLayout>
39 #include <QtGui/QLabel>
40 #include <QtGui/QFontMetrics>
41 
42 // TODO: make status bar capable to hide entries if size is too small, use priorisation
43 
44 namespace Kasten2
45 {
46 
47 ViewStatusController::ViewStatusController( StatusBar* statusBar )
48  : mByteArrayView( 0 ), mStatusBar( statusBar )
49 {
50  mPrintFunction = Okteta::OffsetFormat::printFunction( Okteta::OffsetFormat::Hexadecimal );
51 
52  mOffsetLabel = new QLabel( statusBar );
53  statusBar->addWidget( mOffsetLabel );
54 
55  mSelectionLabel = new QLabel( statusBar );
56  statusBar->addWidget( mSelectionLabel );
57 
58  const QString insertModeText = i18nc( "@info:status short for: Insert mode", "INS" );
59  const QString overwriteModeText = i18nc( "@info:status short for: Overwrite mode", "OVR" );
60  const QString insertModeTooltip = i18nc( "@info:tooltip", "Insert mode" );
61  const QString overwriteModeTooltip = i18nc( "@info:tooltip", "Overwrite mode" );
62  mOverwriteModeToggleButton = new ToggleButton( insertModeText, insertModeTooltip, statusBar );
63  mOverwriteModeToggleButton->setCheckedState( overwriteModeText, overwriteModeTooltip );
64  statusBar->addWidget( mOverwriteModeToggleButton );
65  connect( mOverwriteModeToggleButton, SIGNAL(clicked(bool)), SLOT(setOverwriteMode(bool)) );
66 
67  mValueCodingComboBox = new KComboBox( statusBar );
68  QStringList list;
69  list.append( i18nc("@item:inmenu encoding of the bytes as values in the hexadecimal format","Hexadecimal") );
70  list.append( i18nc("@item:inmenu encoding of the bytes as values in the decimal format", "Decimal") );
71  list.append( i18nc("@item:inmenu encoding of the bytes as values in the octal format", "Octal") );
72  list.append( i18nc("@item:inmenu encoding of the bytes as values in the binary format", "Binary") );
73  mValueCodingComboBox->addItems( list );
74  mValueCodingComboBox->setToolTip(
75  i18nc("@info:tooltip","Coding of the value interpretation in the current view.") );
76  connect( mValueCodingComboBox, SIGNAL(activated(int)), SLOT(setValueCoding(int)) );
77  statusBar->addWidget( mValueCodingComboBox );
78 
79  mCharCodingComboBox = new KComboBox( statusBar );
80  mCharCodingComboBox->addItems( Okteta::CharCodec::codecNames() );
81  mCharCodingComboBox->setToolTip(
82  i18nc("@info:tooltip","Encoding in the character column of the current view.") );
83  connect( mCharCodingComboBox, SIGNAL(activated(int)), SLOT(setCharCoding(int)) );
84  statusBar->addWidget( mCharCodingComboBox );
85 
86  fixWidths( 0 );
87 
88  setTargetModel( 0 );
89 }
90 
91 
92 // the letter C can be very wide, that is why with proportinal fonts there seems too much space used, but isn't
93 // see http://frinring.wordpress.com/2008/10/14/better-width-with-open-sources/
94 void ViewStatusController::fixWidths( int offsetCoding )
95 {
96  const QFontMetrics metrics = mStatusBar->fontMetrics();
97 
98  // mOffsetLabel
99  static const int hexDigitsCount = 16;
100  static const int decimalDigitsCount = 10;
101  static const int firstLetterIndex = 10;
102  static const char digits[hexDigitsCount] =
103  {
104  '0','1','2','3','4','5','6','7','8','9',
105  'A','B','C','D','E','F'
106  };
107 
108  int largestOffsetWidth = 0;
109  int largestSelectionWidth = 0;
110  int widestDigitIndex = 0;
111  const int digitsCount = (offsetCoding == 0) ? hexDigitsCount : decimalDigitsCount;
112  for( int i=0; i<digitsCount; ++i )
113  {
114  QString offset;
115  if( offsetCoding == 0 )
116  {
117  offset = QString( 9, QLatin1Char(digits[i]) );
118  offset[4] = QLatin1Char(':');
119  }
120  else
121  {
122  offset = QString( 10, QLatin1Char(digits[i]) );
123  }
124 
125  const QString offsetText = i18n( "Offset: %1", offset );
126  const int offsetWidth = metrics.boundingRect( offsetText ).width();
127  if( largestOffsetWidth < offsetWidth )
128  largestOffsetWidth = offsetWidth;
129 
130  const char countDigit = (i<firstLetterIndex) ? digits[i] : digits[widestDigitIndex];
131  const int maxNumber = QByteArray('1'+QByteArray(9,countDigit)).toInt();
132  const QString bytesCount = i18n( "%1 bytes", maxNumber );
133  const QString selectionString = i18nc( "@info:status selection: start offset - end offset ()",
134  "Selection: %1 - %2 (%3)", offset, offset, bytesCount );
135 
136  const int selectionWidth = metrics.boundingRect( selectionString ).width();
137  if( largestSelectionWidth < selectionWidth )
138  {
139  if( i < firstLetterIndex )
140  widestDigitIndex = i;
141  largestSelectionWidth = selectionWidth;
142  }
143  }
144  mOffsetLabel->setFixedWidth( largestOffsetWidth );
145  mSelectionLabel->setFixedWidth( largestSelectionWidth );
146  mStatusBar->updateLayout();
147 }
148 
149 void ViewStatusController::setTargetModel( AbstractModel* model )
150 {
151  if( mByteArrayView )
152  {
153  mByteArrayView->disconnect( this );
154  mByteArrayView->disconnect( mOverwriteModeToggleButton );
155  }
156 
157  mByteArrayView = model ? model->findBaseModel<ByteArrayView*>() : 0;
158 
159  const bool hasView = ( mByteArrayView != 0 );
160  if( hasView )
161  {
162  mStartOffset = mByteArrayView->startOffset();
163 
164  onCursorPositionChanged( mByteArrayView->cursorPosition() );
165  onSelectedDataChanged( mByteArrayView->modelSelection() );
166  mOverwriteModeToggleButton->setChecked( mByteArrayView->isOverwriteMode() );
167  onOffsetCodingChanged( mByteArrayView->offsetCoding() );
168  onValueCodingChanged( mByteArrayView->valueCoding() );
169  onCharCodecChanged( mByteArrayView->charCodingName() );
170 
171  connect( mByteArrayView, SIGNAL(cursorPositionChanged(Okteta::Address)), SLOT(onCursorPositionChanged(Okteta::Address)) );
172  connect( mByteArrayView, SIGNAL(selectedDataChanged(const Kasten2::AbstractModelSelection*)),
173  SLOT(onSelectedDataChanged(const Kasten2::AbstractModelSelection*)) );
174  connect( mByteArrayView, SIGNAL(overwriteModeChanged(bool)),
175  mOverwriteModeToggleButton, SLOT(setChecked(bool)) );
176  connect( mByteArrayView, SIGNAL(offsetCodingChanged(int)), SLOT(onOffsetCodingChanged(int)) );
177  connect( mByteArrayView, SIGNAL(valueCodingChanged(int)), SLOT(onValueCodingChanged(int)) );
178  connect( mByteArrayView, SIGNAL(charCodecChanged(QString)),
179  SLOT(onCharCodecChanged(QString)) );
180  }
181  else
182  {
183  mOffsetLabel->setText( i18nc("@info:status offset value not available", "Offset: -") );
184  mSelectionLabel->setText( i18nc("@info:status offset value not available", "Selection: -") );
185  mOverwriteModeToggleButton->setChecked( false );
186  mValueCodingComboBox->setCurrentIndex( 0 );
187  mCharCodingComboBox->setCurrentIndex( 0 );
188  }
189 
190  mOffsetLabel->setEnabled( hasView );
191  mSelectionLabel->setEnabled( hasView );
192  mOverwriteModeToggleButton->setEnabled( hasView );
193  mValueCodingComboBox->setEnabled( hasView );
194  mCharCodingComboBox->setEnabled( hasView );
195 }
196 
197 void ViewStatusController::setOverwriteMode( bool overwrite )
198 {
199  mByteArrayView->setOverwriteMode( overwrite );
200 }
201 
202 void ViewStatusController::setValueCoding( int valueCoding )
203 {
204  mByteArrayView->setValueCoding( valueCoding );
205  mByteArrayView->setFocus();
206 }
207 
208 void ViewStatusController::setCharCoding( int charCoding )
209 {
210  mByteArrayView->setCharCoding( Okteta::CharCodec::codecNames()[charCoding] );
211  mByteArrayView->setFocus();
212 }
213 
214 void ViewStatusController::onCursorPositionChanged( Okteta::Address offset )
215 {
216  char codedOffset[Okteta::OffsetFormat::MaxFormatWidth+1];
217 
218  mPrintFunction( codedOffset, mStartOffset + offset );
219 
220  mOffsetLabel->setText( i18n("Offset: %1", QLatin1String(codedOffset)) );
221 }
222 
223 // TODO: fix selection by cursor not sending updates
224 void ViewStatusController::onSelectedDataChanged( const Kasten2::AbstractModelSelection* modelSelection )
225 {
226  const ByteArraySelection* byteArraySelection = static_cast<const ByteArraySelection*>( modelSelection );
227  const Okteta::AddressRange selection = byteArraySelection->range();
228 
229  QString selectionString;
230  if( ! selection.isEmpty() )
231  {
232  char codedSelectionStart[Okteta::OffsetFormat::MaxFormatWidth+1];
233  char codedSelectionEnd[Okteta::OffsetFormat::MaxFormatWidth+1];
234 
235  mPrintFunction( codedSelectionStart, mStartOffset + selection.start() );
236  mPrintFunction( codedSelectionEnd, mStartOffset + selection.end() );
237 
238  const QString bytesCount = i18np( "1 byte", "%1 bytes", selection.width() );
239  selectionString = i18nc( "@info:status selection: start offset - end offset (number of bytes)",
240  "Selection: %1 - %2 (%3)", QLatin1String(codedSelectionStart), QLatin1String(codedSelectionEnd), bytesCount );
241  }
242  else
243  selectionString = i18nc( "@info:status offset value not available", "Selection: -" );
244 
245  mSelectionLabel->setText( selectionString );
246 }
247 
248 void ViewStatusController::onOffsetCodingChanged( int offsetCoding )
249 {
250  mPrintFunction = Okteta::OffsetFormat::printFunction( (Okteta::OffsetFormat::Format)offsetCoding );
251  fixWidths( offsetCoding );
252 
253  // trigger updates of offset printing labels
254  onCursorPositionChanged( mByteArrayView->cursorPosition() );
255  onSelectedDataChanged( mByteArrayView->modelSelection() );
256 }
257 
258 void ViewStatusController::onValueCodingChanged( int valueCoding )
259 {
260  mValueCodingComboBox->setCurrentIndex( valueCoding );
261 }
262 
263 void ViewStatusController::onCharCodecChanged( const QString& charCodecName )
264 {
265  const int charCodingIndex = Okteta::CharCodec::codecNames().indexOf( charCodecName );
266 
267  mCharCodingComboBox->setCurrentIndex( charCodingIndex );
268 }
269 
270 }
Kasten2::ByteArrayView::setFocus
virtual void setFocus()
Definition: bytearrayview.cpp:155
Okteta::Address
qint32 Address
Definition: address.h:34
Kasten2::ByteArrayView::valueCoding
int valueCoding() const
Definition: bytearrayview.cpp:250
Kasten2::ByteArrayView::charCodingName
QString charCodingName() const
Definition: bytearrayview.cpp:255
Kasten2::ByteArrayView::setValueCoding
void setValueCoding(int valueCoding)
Definition: bytearrayview.cpp:260
KDE::NumberRange< Address, Size >
Kasten2::ByteArrayView::isOverwriteMode
bool isOverwriteMode() const
Definition: bytearrayview.cpp:311
KDE::Range::start
T start() const
Definition: range.h:86
Kasten2::StatusBar
Definition: statusbar.h:40
Okteta::OffsetFormat::Format
Format
Definition: offsetformat.h:43
Kasten2::ByteArrayView::setCharCoding
void setCharCoding(const QString &charCodingName)
Definition: bytearrayview.cpp:265
statusbar.h
KDE::NumberRange::width
S width() const
Definition: numberrange.h:141
Okteta::CharCodec::codecNames
static const QStringList & codecNames()
Definition: charcodec.cpp:35
Kasten2::ByteArrayView::modelSelection
virtual const AbstractModelSelection * modelSelection() const
Definition: bytearrayview.cpp:145
viewstatuscontroller.h
Okteta::OffsetFormat::MaxFormatWidth
static const int MaxFormatWidth
Definition: offsetformat.h:45
Kasten2::ByteArrayView::cursorPosition
Okteta::Address cursorPosition() const
Definition: bytearrayview.cpp:227
Okteta::OffsetFormat::printFunction
static print printFunction(int i)
Definition: offsetformat.h:73
oktetacore.h
Kasten2::StatusBar::updateLayout
void updateLayout()
Definition: statusbar.cpp:52
Okteta::OffsetFormat::Hexadecimal
Definition: offsetformat.h:43
KDE::Range::end
T end() const
Definition: range.h:88
Kasten2::ToggleButton::setCheckedState
void setCheckedState(const KIcon &icon, const QString &text, const QString &toolTip)
Definition: togglebutton.cpp:49
Kasten2::AbstractModelSelection
Definition: abstractmodelselection.h:35
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
Kasten2::ViewStatusController::setTargetModel
virtual void setTargetModel(AbstractModel *model)
Definition: viewstatuscontroller.cpp:149
Kasten2::ByteArrayView::setOverwriteMode
void setOverwriteMode(bool overwriteMode)
Definition: bytearrayview.cpp:400
Kasten2::ToggleButton
Definition: togglebutton.h:40
Kasten2::StatusBar::addWidget
void addWidget(QWidget *widget)
Definition: statusbar.cpp:47
Kasten2::AbstractModel
Definition: abstractmodel.h:40
Kasten2::ByteArrayView::offsetCoding
int offsetCoding() const
Definition: bytearrayview.cpp:296
Kasten2::ByteArrayView::startOffset
Okteta::Address startOffset() const
Definition: bytearrayview.cpp:236
Kasten2::ByteArrayView
Definition: bytearrayview.h:51
Kasten2::ViewStatusController::ViewStatusController
ViewStatusController(StatusBar *statusBar)
Definition: viewstatuscontroller.cpp:47
KDE::Range::isEmpty
bool isEmpty() const
returns true if the range has not been set
Definition: range.h:124
togglebutton.h
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