• 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
  • structures
structview.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Okteta Kasten Framework, made within the KDE community.
3  *
4  * Copyright 2009, 2010, 2012 Alex Richardson <alex.richardson@gmx.de>
5  * Copyright 2009 Friedrich W. H. Kossebau <kossebau@kde.org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) version 3, or any
11  * later version accepted by the membership of KDE e.V. (or its
12  * successor approved by the membership of KDE e.V.), which shall
13  * act as a proxy defined in Section 6 of version 3 of the license.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "structview.h"
25 
26 // controller
27 #include "structtreemodel.h"
28 #include "structtool.h"
29 #include "structuresmanager.h"
30 #include "structviewitemdelegate.h"
31 //settings
32 #include "structviewpreferences.h"
33 #include "settings/structviewdisplaysettingswidget.h"
34 #include "settings/structuresmanagerview.h"
35 #include "settings/structureaddremovewidget.h"
36 
37 #include "script/scriptutils.h"
38 #include "script/scriptloggerview.h"
39 
40 //#include "modeltest.h"
41 
42 // KDE
43 #include <KComboBox>
44 #include <KLocale>
45 #include <KConfigDialog>
46 #include <KPushButton>
47 
48 // Qt
49 #include <QtGui/QLabel>
50 #include <QtGui/QLayout>
51 #include <QtGui/QTreeView>
52 #include <QtGui/QHeaderView>
53 #include <QFocusEvent>
54 
55 namespace Kasten2
56 {
57 
58 StructView::StructView(StructTool* tool, QWidget* parent) :
59  QWidget(parent), mTool(tool), mDelegate(new StructViewItemDelegate(this)),
60  mStructTreeViewFocusChild(0)
61 {
62  QBoxLayout* baseLayout = new QVBoxLayout(this);
63  setLayout(baseLayout);
64  baseLayout->setMargin(0);
65  // table
66  mStructTreeModel = new StructTreeModel(mTool, this);
67  // mModeltest = new ModelTest(mStructTreeModel, this);
68  mStructTreeView = new QTreeView(this);
69  mStructTreeView->setObjectName( QLatin1String("StructTree" ));
70  mStructTreeView->setRootIsDecorated(true);
71  mStructTreeView->setAlternatingRowColors(true);
72  mStructTreeView->setItemsExpandable(true);
73  mStructTreeView->setUniformRowHeights(true);
74  mStructTreeView->setAllColumnsShowFocus(true);
75  mStructTreeView->setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::EditKeyPressed);
76  mStructTreeView->setItemDelegate(mDelegate);
77  mStructTreeView->setDragEnabled(false);
78  mStructTreeView->setSortingEnabled(false);
79  mStructTreeView->setModel(mStructTreeModel);
80  mStructTreeView->setHeaderHidden(false);
81  mStructTreeView->setSortingEnabled(false);
82  mStructTreeView->installEventFilter(this);
83  QHeaderView* header = mStructTreeView->header();
84  header->setResizeMode(QHeaderView::Interactive);
85 
86  baseLayout->addWidget(mStructTreeView, 10);
87 
88  // settings
89  QBoxLayout* settingsLayout = new QHBoxLayout();
90  settingsLayout->setMargin(0);
91 
92  baseLayout->addLayout(settingsLayout);
93 
94  KIcon validateIcon = KIcon(QLatin1String("document-sign"));
95  mValidateButton = new KPushButton(validateIcon, i18nc("@action:button", "Validate"), this);
96  const QString validationToolTip = i18nc("@info:tooltip", "Validate all structures.");
97  mValidateButton->setToolTip(validationToolTip);
98  mValidateButton->setEnabled(false); //no point validating without file open
99  connect(mValidateButton, SIGNAL(clicked()), mTool, SLOT(validateAllStructures()));
100  connect(mTool, SIGNAL(byteArrayModelChanged(Okteta::AbstractByteArrayModel*)),
101  this, SLOT(onByteArrayModelChanged(Okteta::AbstractByteArrayModel*)));
102  //TODO also disable the button if the structure has no validatable members
103  settingsLayout->addWidget(mValidateButton);
104 
105  mLockStructureButton = new KPushButton(this);
106  mLockStructureButton->setCheckable(true);
107  setLockButtonState(false);
108  mLockStructureButton->setEnabled(false); //won't work at beginning
109  connect(mLockStructureButton, SIGNAL(toggled(bool)), this, SLOT(lockButtonToggled()));
110 
111  settingsLayout->addWidget(mLockStructureButton);
112 
113  settingsLayout->addStretch(); //stretch before the settings button
114 
115  KIcon console = KIcon(QLatin1String("utilities-terminal"));
116  mScriptConsoleButton = new KPushButton(console, i18nc("@action:button", "Script console"), this);
117  mScriptConsoleButton->setToolTip(i18nc("@info:tooltip", "Open script console."));
118  connect(mScriptConsoleButton, SIGNAL(pressed()), this, SLOT(openScriptConsole()));
119  settingsLayout->addWidget(mScriptConsoleButton);
120 
121  KIcon settings = KIcon(QLatin1String("configure"));
122  mSettingsButton = new KPushButton(settings, i18nc("@action:button", "Settings"), this);
123  const QString settingsTooltip = i18nc("@info:tooltip", "Open settings.");
124  mSettingsButton->setToolTip(settingsTooltip);
125  connect(mSettingsButton, SIGNAL(pressed()), this, SLOT(openSettingsDlg()));
126  settingsLayout->addWidget(mSettingsButton);
127 
128  connect(mStructTreeView->selectionModel(),
129  SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
130  SLOT(onCurrentRowChanged(QModelIndex,QModelIndex)));
131 
132  connect(mTool, SIGNAL(cursorIndexChanged()), SLOT(onCursorIndexChange()));
133 }
134 
135 void StructView::onCursorIndexChange()
136 {
137  QModelIndex idx = mStructTreeView->currentIndex();
138  if (idx.isValid())
139  mTool->mark(idx);
140 }
141 
142 void StructView::openSettingsDlg(int page)
143 {
144  //An instance of your dialog could be already created and could be cached,
145  //in which case you want to display the cached dialog instead of creating
146  //another one
147  if (KConfigDialog::showDialog(QLatin1String("Structures Tool Settings")))
148  return;
149 
150  //KConfigDialog didn't find an instance of this dialog, so lets create it :
151  KConfigDialog* dialog = new KConfigDialog(this, QLatin1String("Structures Tool Settings"),
152  StructViewPreferences::self());
153  StructViewDisplaySettingsWidget* displaySettings = new StructViewDisplaySettingsWidget();
154  QWidget* structSelectionWrapper = new QWidget();
155  StructuresManagerView* structureSettings = new StructuresManagerView(mTool, this);
156  KPageWidgetItem* displ = dialog->addPage(displaySettings, i18n("Value Display"),
157  QLatin1String("configure"));
158  QHBoxLayout* hbox = new QHBoxLayout();
159  structSelectionWrapper->setLayout(hbox);
160  hbox->addWidget(structureSettings);
161  Q_ASSERT(structureSettings->objectName() == QLatin1String("kcfg_LoadedStructures"));
162  KPageWidgetItem* management = dialog->addPage(structSelectionWrapper, i18n("Structures management"),
163  QLatin1String("preferences-plugin"));
164 
165  //User edited the configuration - update your local copies of the configuration data
166  connect(dialog, SIGNAL(settingsChanged(QString)), mTool, SLOT(setSelectedStructuresInView()));
167  connect(dialog, SIGNAL(settingsChanged(QString)), this, SLOT(update()));
168 
169  //XXX once kconfig_compiler signals work with settings dialog, use that
170  if (page == 0)
171  dialog->setCurrentPage(displ);
172  else if (page == 1)
173  dialog->setCurrentPage(management);
174 
175  dialog->show();
176 }
177 
178 bool StructView::eventFilter(QObject* object, QEvent* event)
179 {
180  if (object == mStructTreeView)
181  {
182  if (event->type() == QEvent::FocusIn)
183  {
184  const QModelIndex current = mStructTreeView->selectionModel()->currentIndex();
185 
186  if (current.isValid())
187  mTool->mark(current);
188  else
189  mTool->unmark();
190 
191  //set state of lock button
192  setLockButtonState(mTool->isStructureLocked(current));
193  mLockStructureButton->setEnabled(mTool->canStructureBeLocked(current));
194  }
195  else if (event->type() == QEvent::FocusOut)
196  {
197  QWidget* treeViewFocusWidget = mStructTreeView->focusWidget();
198  const bool subChildHasFocus = (treeViewFocusWidget != mStructTreeView);
199  if (subChildHasFocus)
200  {
201  mStructTreeViewFocusChild = treeViewFocusWidget;
202  mStructTreeViewFocusChild->installEventFilter(this);
203  }
204  else
205  mTool->unmark();
206  }
207  }
208  else if (object == mStructTreeViewFocusChild)
209  {
210  // TODO: it is only assumed the edit widget will be removed if it loses the focus
211  if (event->type() == QEvent::FocusOut)
212  {
213  if (!mStructTreeView->hasFocus())
214  mTool->unmark();
215  mStructTreeViewFocusChild->removeEventFilter(this);
216  mStructTreeViewFocusChild = 0;
217  }
218  }
219 
220  return QWidget::eventFilter(object, event);
221 }
222 
223 void StructView::onCurrentRowChanged(const QModelIndex& current, const QModelIndex& previous)
224 {
225  Q_UNUSED( previous )
226  if (current.isValid() && mTool->byteArrayModel())
227  {
228  mTool->mark(current);
229  mLockStructureButton->setEnabled(true);
230  setLockButtonState(mTool->isStructureLocked(current));
231  }
232  else
233  mTool->unmark();
234 }
235 
236 StructView::~StructView()
237 {
238 }
239 
240 void StructView::lockButtonToggled()
241 {
242  setLockButtonState(mLockStructureButton->isChecked());
243  const QModelIndex current = mStructTreeView->selectionModel()->currentIndex();
244  if (!current.isValid())
245  {
246  kWarning() << "it should not be possible to toggle this button when current index is invalid!";
247  return;
248  }
249 
250  if (mLockStructureButton->isChecked())
251  mTool->lockStructure(current);
252  else
253  mTool->unlockStructure(current);
254 }
255 
256 void StructView::setLockButtonState(bool structureLocked)
257 {
258  if (structureLocked)
259  {
260  mLockStructureButton->setIcon(KIcon(QLatin1String("object-locked")));
261  mLockStructureButton->setText(i18nc("@action:pushbutton"
262  " unlock the starting offset of the current structure", "Unlock"));
263  mLockStructureButton->setToolTip(i18nc("@info:tooltip",
264  "Unlock selected structure, i.e. the starting offset is"
265  " always set to the current cursor position."));
266  }
267  else
268  {
269  mLockStructureButton->setIcon(KIcon(QLatin1String("object-unlocked")));
270  mLockStructureButton->setText(i18nc("@action:pushbutton"
271  " unlock the starting offset of the current structure", "Lock"));
272  mLockStructureButton->setToolTip(i18nc("@info:tooltip",
273  "Lock selected structure to current offset."));
274  }
275  mLockStructureButton->setChecked(structureLocked);
276 }
277 
278 void StructView::openScriptConsole()
279 {
280  KDialog* dialog = new KDialog(this);
281  dialog->setMainWidget(new ScriptLoggerView(mTool->allData()));
282  dialog->show();
283 }
284 
285 void StructView::onByteArrayModelChanged(Okteta::AbstractByteArrayModel* model)
286 {
287  const bool validModel = model != 0;
288  QModelIndex current = mStructTreeView->currentIndex();
289  mLockStructureButton->setEnabled(mTool->canStructureBeLocked(current));
290  setLockButtonState(mTool->isStructureLocked(current));
291  mValidateButton->setEnabled(validModel);
292 }
293 
294 }
Kasten2::StructView::setLockButtonState
void setLockButtonState(bool structureLocked)
Definition: structview.cpp:256
Kasten2::StructView::mScriptConsoleButton
KPushButton * mScriptConsoleButton
Definition: structview.h:66
Okteta::AbstractByteArrayModel
could it be useful to hide the data access behind an iterator? * class KDataBufferIterator { public: ...
Definition: abstractbytearraymodel.h:79
structviewitemdelegate.h
Kasten2::StructView::mStructTreeModel
StructTreeModel * mStructTreeModel
Definition: structview.h:60
Kasten2::StructView::mStructTreeView
QTreeView * mStructTreeView
Definition: structview.h:63
QWidget
Kasten2::StructTool::unlockStructure
void unlockStructure(const QModelIndex &idx)
Definition: structtool.cpp:389
structviewpreferences.h
structview.h
Kasten2::StructView::mStructTreeViewFocusChild
QWidget * mStructTreeViewFocusChild
Definition: structview.h:69
KDialog
Kasten2::StructTool::canStructureBeLocked
bool canStructureBeLocked(const QModelIndex &idx) const
check if there is any ByteArrayModel available to lock the structure
Definition: structtool.cpp:420
QObject
structuresmanagerview.h
Kasten2::StructView::~StructView
virtual ~StructView()
Definition: structview.cpp:236
Kasten2::StructTool::lockStructure
void lockStructure(const QModelIndex &idx)
Definition: structtool.cpp:376
ScriptLoggerView
Definition: scriptloggerview.h:32
Kasten2::StructTool::byteArrayModel
Okteta::AbstractByteArrayModel * byteArrayModel() const
Definition: structtool.cpp:431
Kasten2::StructTool::allData
TopLevelDataInformation::List allData() const
Definition: structtool.cpp:446
Kasten2::StructView::onByteArrayModelChanged
void onByteArrayModelChanged(Okteta::AbstractByteArrayModel *model)
Definition: structview.cpp:285
Kasten2::StructView::openScriptConsole
void openScriptConsole()
Definition: structview.cpp:278
Kasten2::StructView::openSettingsDlg
void openSettingsDlg(int page=0)
Definition: structview.cpp:142
Kasten2::StructView::mLockStructureButton
KPushButton * mLockStructureButton
Definition: structview.h:68
structureaddremovewidget.h
Kasten2::StructTool::isStructureLocked
bool isStructureLocked(const QModelIndex &idx) const
Definition: structtool.cpp:406
Kasten2::StructView::onCurrentRowChanged
void onCurrentRowChanged(const QModelIndex &current, const QModelIndex &previous)
Definition: structview.cpp:223
Kasten2::StructView::StructView
StructView(StructTool *tool, QWidget *parent=0)
Definition: structview.cpp:58
structtreemodel.h
structtool.h
Kasten2::StructViewPreferences::self
static StructViewPreferences * self()
Definition: structviewpreferences.cpp:25
Kasten2::StructView::mValidateButton
KPushButton * mValidateButton
Definition: structview.h:64
Kasten2::StructView::mSettingsButton
KPushButton * mSettingsButton
Definition: structview.h:65
structuresmanager.h
Kasten2::StructView::eventFilter
virtual bool eventFilter(QObject *object, QEvent *event)
Definition: structview.cpp:178
scriptloggerview.h
Kasten2::StructTreeModel
Definition: structtreemodel.h:36
Kasten2::StructTool::unmark
void unmark()
Definition: structtool.cpp:349
StructuresManagerView
Definition: structuresmanagerview.h:45
Kasten2::StructView::lockButtonToggled
void lockButtonToggled()
Definition: structview.cpp:240
StructViewDisplaySettingsWidget
Definition: structviewdisplaysettingswidget.h:29
structviewdisplaysettingswidget.h
Kasten2::StructView::mTool
StructTool * mTool
Definition: structview.h:58
Kasten2::StructView::onCursorIndexChange
void onCursorIndexChange()
Definition: structview.cpp:135
scriptutils.h
Kasten2::StructTool
Definition: structtool.h:49
Kasten2::StructTool::mark
void mark(const QModelIndex &idx)
Definition: structtool.cpp:331
StructViewItemDelegate
Definition: structviewitemdelegate.h:28
Kasten2::StructView::mDelegate
StructViewItemDelegate * mDelegate
Definition: structview.h:67
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