• 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
  • stringsextract
stringsextractview.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 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 "stringsextractview.h"
24 
25 // tool
26 #include "containedstringtablemodel.h"
27 #include "stringsextracttool.h"
28 // KDE
29 #include <KPushButton>
30 #include <KLineEdit>
31 #include <KGuiItem>
32 #include <KLocale>
33 #include <KGlobalSettings>
34 // Qt
35 #include <QtGui/QLabel>
36 #include <QtGui/QLayout>
37 #include <QtGui/QSpinBox>
38 #include <QtGui/QSortFilterProxyModel>
39 #include <QtGui/QHeaderView>
40 #include <QtGui/QTreeView>
41 #include <QtGui/QClipboard>
42 #include <QtGui/QApplication>
43 #include <QtGui/QFocusEvent>
44 
45 
46 namespace Kasten2
47 {
48 
49 static const int MinimumStringLength = 1;
50 
51 StringsExtractView::StringsExtractView( StringsExtractTool *tool, QWidget* parent )
52  : QWidget( parent ), mTool( tool )
53 {
54  QVBoxLayout *baseLayout = new QVBoxLayout( this );
55  baseLayout->setMargin( 0 );
56 
57  // update
58  QHBoxLayout *updateLayout = new QHBoxLayout();
59 
60  updateLayout->addStretch();
61  QLabel *label = new QLabel( i18nc("@label:spinbox minimum length for consecutive chars to be seen as a string",
62  "Minimum length:"), this );
63  updateLayout->addWidget( label );
64 
65  mMinLengthSpinBox = new QSpinBox( this );
66  mMinLengthSpinBox->setValue( mTool->minLength() );
67  mMinLengthSpinBox->setMinimum( MinimumStringLength );
68  connect( mMinLengthSpinBox, SIGNAL(valueChanged(int)), mTool, SLOT(setMinLength(int)) );
69  label->setBuddy( mMinLengthSpinBox );
70  updateLayout->addWidget( mMinLengthSpinBox );
71 
72  const KGuiItem updateGuiItem =
73  KGuiItem( i18nc("@action:button extract the strings from the byte array","&Extract"),
74  QLatin1String("document-export"),
75  i18nc("@info:tooltip",
76  "Finds the strings contained in the selected range and lists them in the view below."),
77  i18nc("@info:whatsthis",
78  "If you press the <interface>Extract</interface> button, "
79  "the selected range is searched for all strings which have the set minimum length. "
80  "This strings found will be listed in the view below.") );
81  mUpdateButton = new KPushButton( updateGuiItem, this );
82  mUpdateButton->setEnabled( mTool->isApplyable() );
83  connect( mUpdateButton, SIGNAL(clicked(bool)), mTool, SLOT(extractStrings()) );
84  updateLayout->addWidget( mUpdateButton );
85 
86  baseLayout->addLayout( updateLayout );
87 
88  // filter
89  QHBoxLayout *filterLayout = new QHBoxLayout();
90 
91  label = new QLabel( i18nc("@label:lineedit filter term for displayed strings","Filter:"), this );
92  filterLayout->addWidget( label );
93 
94  KLineEdit *mFilterEdit = new KLineEdit( this );
95  mFilterEdit->setClearButtonShown( true );
96  mFilterEdit->setClickMessage( i18n("Enter a term to limit the list.") );
97  label->setBuddy( mFilterEdit );
98  filterLayout->addWidget( mFilterEdit, 10 );
99 
100  baseLayout->addLayout( filterLayout );
101 
102  // strings
103  mContainedStringTableModel =
104  new ContainedStringTableModel( mTool->containedStringList(), mTool->offsetCoding(), this );
105  connect( mTool, SIGNAL(offsetCodingChanged(int)),
106  mContainedStringTableModel, SLOT(setOffsetCoding(int)) );
107 
108  mSortFilterProxyModel = new QSortFilterProxyModel( this );
109  mSortFilterProxyModel->setDynamicSortFilter( true );
110  mSortFilterProxyModel->setSourceModel( mContainedStringTableModel );
111  mSortFilterProxyModel->setFilterKeyColumn( ContainedStringTableModel::StringColumnId );
112  mSortFilterProxyModel->setFilterCaseSensitivity( Qt::CaseInsensitive );
113  connect( mFilterEdit, SIGNAL(textChanged(QString)),
114  mSortFilterProxyModel, SLOT(setFilterFixedString(QString)) );
115 
116  mContainedStringTableView = new QTreeView( this );
117  connect( KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
118  SLOT(setFixedFontByGlobalSettings()) );
119  setFixedFontByGlobalSettings(); //do this before setting model
120  mContainedStringTableView->setObjectName( QLatin1String( "ContainedStringTable" ) );
121  mContainedStringTableView->setRootIsDecorated( false );
122  mContainedStringTableView->setItemsExpandable( false );
123  mContainedStringTableView->setUniformRowHeights( true );
124  mContainedStringTableView->setAllColumnsShowFocus( true );
125  mContainedStringTableView->setSelectionMode( QAbstractItemView::ExtendedSelection );
126  mContainedStringTableView->setSortingEnabled( true );
127  mContainedStringTableView->installEventFilter( this );
128  QHeaderView* header = mContainedStringTableView->header();
129  header->setFont( font() );
130  header->setResizeMode( QHeaderView::Interactive );
131  mContainedStringTableView->setModel( mSortFilterProxyModel );
132  mContainedStringTableView->sortByColumn( ContainedStringTableModel::OffsetColumnId, Qt::AscendingOrder );
133  connect( mContainedStringTableView, SIGNAL(doubleClicked(QModelIndex)),
134  SLOT(onStringDoubleClicked(QModelIndex)) );
135  connect( mContainedStringTableView->selectionModel(),
136  SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
137  SLOT(onStringSelectionChanged()) );
138 
139  baseLayout->addWidget( mContainedStringTableView, 10 );
140 
141  // actions
142  QHBoxLayout *actionsLayout = new QHBoxLayout();
143 
144  const KGuiItem copyGuiItem =
145  KGuiItem( i18n("C&opy"),
146  QLatin1String("edit-copy"),
147  i18nc("@info:tooltip",
148  "Copies the selected strings to the clipboard."),
149  i18nc("@info:whatsthis",
150  "If you press the <interface>Copy</interface> button, all strings you selected "
151  "in the list are copied to the clipboard.") );
152  mCopyButton = new KPushButton( copyGuiItem, this );
153  connect( mCopyButton, SIGNAL(clicked(bool)), SLOT(onCopyButtonClicked()) );
154  actionsLayout->addWidget( mCopyButton );
155 
156  actionsLayout->addStretch();
157 
158  const KGuiItem gotoGuiItem =
159  KGuiItem( i18n("&Show"),
160  QLatin1String("go-jump"),
161  i18nc("@info:tooltip",
162  "Shows the selected string in the view."),
163  i18nc("@info:whatsthis",
164  "If you press the <interface>Go to</interface> button, the string which was last "
165  "selected is marked and shown in the view.") );
166  mGotoButton = new KPushButton( gotoGuiItem, this );
167  connect( mGotoButton, SIGNAL(clicked(bool)), SLOT(onGotoButtonClicked()) );
168  actionsLayout->addWidget( mGotoButton );
169 
170  baseLayout->addLayout( actionsLayout );
171 
172  connect( mTool, SIGNAL(uptodateChanged(bool)), SLOT(onStringsUptodateChanged(bool)) );
173  connect( mTool, SIGNAL(isApplyableChanged(bool)), SLOT(onApplyableChanged(bool)));
174  connect( mTool, SIGNAL(canHighlightStringChanged(bool)), SLOT(onCanHighlightStringChanged(bool)) );
175 
176  onStringSelectionChanged();
177 }
178 
179 bool StringsExtractView::eventFilter( QObject* object, QEvent* event )
180 {
181  if( object == mContainedStringTableView )
182  {
183  if( event->type() == QEvent::FocusOut )
184  {
185  QFocusEvent* focusEvent = static_cast<QFocusEvent*>( event );
186  const Qt::FocusReason focusReason = focusEvent->reason();
187  if( focusReason != Qt::ActiveWindowFocusReason
188  && focusReason != Qt::PopupFocusReason )
189  mTool->unmarkString();
190  }
191  }
192 
193  return QWidget::eventFilter( object, event );
194 }
195 
196 void StringsExtractView::setFixedFontByGlobalSettings()
197 {
198  mContainedStringTableView->setFont( KGlobalSettings::fixedFont() );
199 }
200 
201 void StringsExtractView::onStringsUptodateChanged( bool stringsUptodate )
202 {
203  if( stringsUptodate )
204  mContainedStringTableModel->update();
205 
206  const bool isApplyable = mTool->isApplyable();
207  mUpdateButton->setEnabled( !stringsUptodate && isApplyable );
208 }
209 
210 void StringsExtractView::onApplyableChanged( bool isApplyable )
211 {
212  mUpdateButton->setEnabled( !mTool->isUptodate() && isApplyable );
213 }
214 
215 void StringsExtractView::onCanHighlightStringChanged( bool canHighlightString )
216 {
217  const bool stringSelected = mContainedStringTableView->selectionModel()->currentIndex().isValid();
218  mGotoButton->setEnabled( canHighlightString && stringSelected );
219 }
220 
221 
222 void StringsExtractView::onGotoButtonClicked()
223 {
224  const QModelIndex index = mContainedStringTableView->selectionModel()->currentIndex();
225  if( index.isValid() )
226  {
227  // TODO: hack: as currently the marking is only undone if the focus leaves the listview it needs to be moved there before
228  mContainedStringTableView->setFocus();
229  onStringDoubleClicked( index );
230  }
231 }
232 
233 void StringsExtractView::onCopyButtonClicked()
234 {
235  const QModelIndexList selectedRows = mContainedStringTableView->selectionModel()->selectedRows();
236  const QList<ContainedString> *containedStringList = mTool->containedStringList();
237 
238  QString strings;
239  foreach( const QModelIndex &index, selectedRows )
240  {
241  const int i = mSortFilterProxyModel->mapToSource(index).row();
242  strings += containedStringList->at( i ).string();
243  strings += QLatin1Char('\n'); //TODO: specific linefeed for platforms
244  }
245  QApplication::clipboard()->setText( strings );
246 }
247 
248 void StringsExtractView::onStringSelectionChanged()
249 {
250  const QItemSelectionModel *selectionModel = mContainedStringTableView->selectionModel();
251 
252  // TODO: selectionModel->selectedIndexes() is a expensive operation,
253  // but with Qt 4.4.3 hasSelection() has the flaw to return true with a current index
254  const bool hasSelection = !selectionModel->selectedIndexes().isEmpty();
255  mCopyButton->setEnabled( hasSelection );
256 
257  const bool stringSelected = selectionModel->isSelected( selectionModel->currentIndex() );
258  const bool canHighlightString = mTool->canHighlightString();
259  mGotoButton->setEnabled( canHighlightString && stringSelected );
260 }
261 
262 void StringsExtractView::onStringDoubleClicked( const QModelIndex &index )
263 {
264  if( mTool->canHighlightString() )
265  mTool->markString( mSortFilterProxyModel->mapToSource(index).row() );
266 }
267 
268 StringsExtractView::~StringsExtractView() {}
269 
270 }
ContainedStringTableModel::StringColumnId
Definition: containedstringtablemodel.h:43
ContainedStringTableModel::update
void update()
Definition: containedstringtablemodel.cpp:45
Kasten2::StringsExtractTool::containedStringList
const QList< ContainedString > * containedStringList() const
Definition: stringsextracttool.h:121
Kasten2::StringsExtractView::~StringsExtractView
virtual ~StringsExtractView()
Definition: stringsextractview.cpp:268
Kasten2::StringsExtractTool::isUptodate
bool isUptodate() const
Definition: stringsextracttool.h:123
QWidget
Kasten2::StringsExtractTool::markString
void markString(int stringId)
Definition: stringsextracttool.cpp:131
ContainedStringTableModel::OffsetColumnId
Definition: containedstringtablemodel.h:42
QObject
stringsextracttool.h
stringsextractview.h
Kasten2::StringsExtractTool::offsetCoding
int offsetCoding() const
Definition: stringsextracttool.cpp:107
Kasten2::StringsExtractTool::isApplyable
bool isApplyable() const
Definition: stringsextracttool.cpp:56
ContainedStringTableModel
Definition: containedstringtablemodel.h:35
Kasten2::StringsExtractTool::minLength
int minLength() const
Definition: stringsextracttool.h:122
Kasten2::StringsExtractTool::canHighlightString
bool canHighlightString() const
Definition: stringsextracttool.cpp:61
Kasten2::StringsExtractView::eventFilter
virtual bool eventFilter(QObject *object, QEvent *event)
Definition: stringsextractview.cpp:179
KLineEdit
Kasten2::StringsExtractView::StringsExtractView
StringsExtractView(StringsExtractTool *tool, QWidget *parent=0)
Definition: stringsextractview.cpp:51
QSpinBox
Kasten2::MinimumStringLength
static const int MinimumStringLength
Definition: stringsextractview.cpp:49
Kasten2::StringsExtractTool
Definition: stringsextracttool.h:51
Kasten2::StringsExtractTool::unmarkString
void unmarkString()
Definition: stringsextracttool.cpp:147
QList< ContainedString >
containedstringtablemodel.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