• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

lokalize

  • sources
  • kde-4.14
  • kdesdk
  • lokalize
  • src
  • cataloglistview
cataloglistview.cpp
Go to the documentation of this file.
1 /* ****************************************************************************
2  This file is part of Lokalize
3 
4  Copyright (C) 2007-2009 by Nick Shaforostoff <shafff@ukr.net>
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License as
8  published by the Free Software Foundation; either version 2 of
9  the License or (at your option) version 3 or any later version
10  accepted by the membership of KDE e.V. (or its successor approved
11  by the membership of KDE e.V.), which shall act as a proxy
12  defined in Section 14 of version 3 of the license.
13 
14  This program 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
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program. If not, see <http://www.gnu.org/licenses/>.
21 
22 **************************************************************************** */
23 
24 #include "cataloglistview.h"
25 #include "catalogmodel.h"
26 #include "catalog.h"
27 #include "project.h"
28 
29 #include <klocale.h>
30 #include <kdebug.h>
31 #include <klineedit.h>
32 #include <KConfigGroup>
33 
34 #include <QTime>
35 #include <QTreeView>
36 #include <QHeaderView>
37 #include <QModelIndex>
38 #include <QToolButton>
39 #include <QVBoxLayout>
40 #include <QAction>
41 #include <QMenu>
42 #include <QShortcut>
43 
44 #include <QMdiSubWindow>
45 #include <QMdiArea>
46 
47 #include <QKeyEvent>
48 
49 class CatalogTreeView: public QTreeView
50 {
51  public:
52  CatalogTreeView(QWidget * parent)
53  : QTreeView(parent) {}
54  ~CatalogTreeView() {}
55 
56  protected:
57  void keyReleaseEvent(QKeyEvent *e) {
58  if (e->key() == Qt::Key_Return && currentIndex().isValid()) {
59  emit clicked(currentIndex());
60  e->accept();
61  } else {
62  QTreeView::keyReleaseEvent(e);
63  }
64  }
65 
66 };
67 
68 
69 CatalogView::CatalogView(QWidget* parent, Catalog* catalog)
70  : QDockWidget ( i18nc("@title:window aka Message Tree","Translation Units"), parent)
71  , m_browser(new CatalogTreeView(this))
72  , m_lineEdit(new KLineEdit(this))
73  , m_model(new CatalogTreeModel(this,catalog))
74  , m_proxyModel(new CatalogTreeFilterModel(this))
75 {
76  setObjectName("catalogTreeView");
77 
78  QWidget* w=new QWidget(this);
79  QVBoxLayout* layout=new QVBoxLayout(w);
80  layout->setContentsMargins(0,0,0,0);
81  QHBoxLayout* l=new QHBoxLayout;
82  l->setContentsMargins(0,0,0,0);
83  l->setSpacing(0);
84  layout->addLayout(l);
85 
86  m_lineEdit->setClearButtonShown(true);
87  m_lineEdit->setClickMessage(i18n("Quick search..."));
88  m_lineEdit->setToolTip(i18nc("@info:tooltip","Activated by Ctrl+L.")+" "+i18nc("@info:tooltip","Accepts regular expressions"));
89  connect (m_lineEdit,SIGNAL(textChanged(QString)),this,SLOT(setFilterRegExp()),Qt::QueuedConnection);
90  // QShortcut* ctrlEsc=new QShortcut(QKeySequence(Qt::META+Qt::Key_Escape),this,SLOT(reset()),0,Qt::WidgetWithChildrenShortcut);
91  QShortcut* esc=new QShortcut(QKeySequence(Qt::Key_Escape),this,0,0,Qt::WidgetWithChildrenShortcut);
92  connect(esc,SIGNAL(activated()),this,SIGNAL(escaped()));
93 
94 
95  QToolButton* btn=new QToolButton(w);
96  btn->setPopupMode(QToolButton::InstantPopup);
97  btn->setText(i18n("options"));
98  //btn->setArrowType(Qt::DownArrow);
99  btn->setMenu(new QMenu(this));
100  m_filterOptionsMenu=btn->menu();
101  connect(m_filterOptionsMenu,SIGNAL(aboutToShow()),this,SLOT(fillFilterOptionsMenu()));
102  connect(m_filterOptionsMenu,SIGNAL(triggered(QAction*)),this,SLOT(filterOptionToggled(QAction*)));
103 
104  l->addWidget(m_lineEdit);
105  l->addWidget(btn);
106  layout->addWidget(m_browser);
107 
108 
109  setTabOrder(m_lineEdit, btn);
110  setTabOrder(btn, m_browser);
111  setFocusProxy(m_lineEdit);
112 
113  setWidget(w);
114 
115  connect(m_browser,SIGNAL(clicked(QModelIndex)),this,SLOT(slotItemActivated(QModelIndex)));
116  m_browser->setRootIsDecorated(false);
117  m_browser->setAllColumnsShowFocus(true);
118  m_browser->setAlternatingRowColors(true);
119  m_browser->viewport()->setBackgroundRole(QPalette::Background);
120 
121  m_proxyModel->setSourceModel(m_model);
122  m_browser->setModel(m_proxyModel);
123  m_browser->setColumnWidth(0,m_browser->columnWidth(0)/3);
124  m_browser->setSortingEnabled(true);
125  m_browser->sortByColumn(0, Qt::AscendingOrder);
126  m_browser->setWordWrap(false);
127  m_browser->setUniformRowHeights(true);
128  m_browser->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
129 
130 
131  KConfig config;
132  KConfigGroup cg(&config,"MainWindow");
133  m_browser->header()->restoreState(QByteArray::fromBase64( cg.readEntry("TreeHeaderState", QByteArray()) ));
134 }
135 
136 CatalogView::~CatalogView()
137 {
138  KConfig config;
139  KConfigGroup cg(&config,"MainWindow");
140  cg.writeEntry("TreeHeaderState",m_browser->header()->saveState().toBase64());
141 }
142 
143 void CatalogView::setFocus()
144 {
145  QDockWidget::setFocus();
146  m_lineEdit->selectAll();
147 }
148 
149 void CatalogView::slotNewEntryDisplayed(const DocPosition& pos)
150 {
151  QModelIndex item=m_proxyModel->mapFromSource(m_model->index(pos.entry,0));
152  m_browser->setCurrentIndex(item);
153  m_browser->scrollTo(item/*,QAbstractItemView::PositionAtCenter*/);
154 }
155 
156 void CatalogView::setFilterRegExp()
157 {
158  QString expr=m_lineEdit->text();
159  if (m_proxyModel->filterRegExp().pattern()!=expr)
160  m_proxyModel->setFilterRegExp(m_proxyModel->filerOptions()&CatalogTreeFilterModel::IgnoreAccel?expr.remove(Project::instance()->accel()):expr);
161 }
162 
163 void CatalogView::slotItemActivated(const QModelIndex& idx)
164 {
165  emit gotoEntry(DocPosition(m_proxyModel->mapToSource(idx).row()),0);
166 }
167 
168 void CatalogView::filterOptionToggled(QAction* action)
169 {
170  if (action->data().isNull())
171  return;
172 
173  int opt=action->data().toInt();
174  if (opt>0)
175  m_proxyModel->setFilerOptions(m_proxyModel->filerOptions()^opt);
176  else
177  {
178  if (opt!=-1) opt=-opt-2;
179  m_proxyModel->setFilterKeyColumn(opt);
180  }
181  m_filterOptionsMenu->clear();
182 }
183 void CatalogView::fillFilterOptionsMenu()
184 {
185  m_filterOptionsMenu->clear();
186 
187  if (m_proxyModel->individualRejectFilterEnabled())
188  m_filterOptionsMenu->addAction(i18n("Reset individual filter"),this,SLOT(setEntriesFilteredOut()));
189 
190 
191  bool extStates=m_model->catalog()->capabilities()&ExtendedStates;
192 
193  const char* const basicTitles[]={
194  I18N_NOOP("Case insensitive"),
195  I18N_NOOP("Ignore accelerator marks"),
196  I18N_NOOP("Ready"),
197  I18N_NOOP("Non-ready"),
198  I18N_NOOP("Non-empty"),
199  I18N_NOOP("Empty"),
200  I18N_NOOP("Changed since file open"),
201  I18N_NOOP("Unchanged since file open"),
202  I18N_NOOP("Same in sync file"),
203  I18N_NOOP("Different in sync file"),
204  I18N_NOOP("Not in sync file")
205  };
206  const char* const* extTitles=Catalog::states();
207  const char* const* alltitles[2]={basicTitles,extTitles};
208 
209  QMenu* basicMenu=m_filterOptionsMenu->addMenu(i18nc("@title:inmenu","Basic"));
210  QMenu* extMenu=extStates?m_filterOptionsMenu->addMenu(i18nc("@title:inmenu","States")):0;
211  QMenu* allmenus[2]={basicMenu,extMenu};
212  QMenu* columnsMenu=m_filterOptionsMenu->addMenu(i18nc("@title:inmenu","Searchable column"));
213 
214  QAction* txt;
215  for (int i=0;(1<<i)<CatalogTreeFilterModel::MaxOption;++i)
216  {
217  bool ext=(1<<i)>=CatalogTreeFilterModel::New;
218  if (!extStates&&ext) break;
219  txt=allmenus[ext]->addAction(i18n(alltitles[ext][i-ext*FIRSTSTATEPOSITION]));
220  txt->setData(1<<i);
221  txt->setCheckable(true);
222  txt->setChecked(m_proxyModel->filerOptions()&(1<<i));
223  if ((1<<i)==CatalogTreeFilterModel::IgnoreAccel)
224  basicMenu->addSeparator();
225  }
226  if (!extStates)
227  m_filterOptionsMenu->addSeparator();
228  for (int i=-1;i<CatalogTreeModel::DisplayedColumnCount;++i)
229  {
230  kWarning()<<i;
231  txt=columnsMenu->addAction((i==-1)?i18nc("@item:inmenu all columns","All"):
232  m_model->headerData(i,Qt::Horizontal,Qt::DisplayRole).toString());
233  txt->setData(-i-2);
234  txt->setCheckable(true);
235  txt->setChecked(m_proxyModel->filterKeyColumn()==i);
236  }
237 }
238 
239 void CatalogView::reset()
240 {
241  m_proxyModel->setFilterKeyColumn(-1);
242  m_proxyModel->setFilerOptions(CatalogTreeFilterModel::AllStates);
243  m_lineEdit->clear();
244  //emit gotoEntry(DocPosition(m_proxyModel->mapToSource(m_browser->currentIndex()).row()),0);
245  slotItemActivated(m_browser->currentIndex());
246 }
247 
248 void CatalogView::setMergeCatalogPointer(MergeCatalog* pointer)
249 {
250  m_proxyModel->setMergeCatalogPointer(pointer);
251 }
252 
253 int CatalogView::siblingEntry(int step)
254 {
255  QModelIndex item=m_browser->currentIndex();
256  int lastRow=m_proxyModel->rowCount()-1;
257  if (!item.isValid())
258  {
259  if (lastRow==-1)
260  return -1;
261  item=m_proxyModel->index((step==1)?0:lastRow,0);
262  m_browser->setCurrentIndex(item);
263  }
264  else
265  {
266  if ( item.row() == ((step==-1)?0:lastRow) )
267  return -1;
268  item=item.sibling(item.row()+step,0);
269  }
270  return m_proxyModel->mapToSource(item).row();
271 }
272 
273 int CatalogView::nextEntry()
274 {
275  return siblingEntry(1);
276 }
277 
278 int CatalogView::prevEntry()
279 {
280  return siblingEntry(-1);
281 }
282 
283 static int edgeEntry(CatalogTreeFilterModel* m_proxyModel, int row)
284 {
285  if (!m_proxyModel->rowCount())
286  return -1;
287 
288  return m_proxyModel->mapToSource(m_proxyModel->index(row,0)).row();
289 }
290 
291 int CatalogView::firstEntry()
292 {
293  return edgeEntry(m_proxyModel,0);
294 }
295 
296 int CatalogView::lastEntry()
297 {
298  return edgeEntry(m_proxyModel,m_proxyModel->rowCount()-1);
299 }
300 
301 
302 void CatalogView::setEntryFilteredOut(int entry, bool filteredOut)
303 {
304  m_proxyModel->setEntryFilteredOut(entry,filteredOut);
305 }
306 
307 void CatalogView::setEntriesFilteredOut(bool filteredOut)
308 {
309  show();
310  m_proxyModel->setEntriesFilteredOut(filteredOut);
311 }
312 
313 
314 #include "cataloglistview.moc"
QWidget::layout
QLayout * layout() const
QSortFilterProxyModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
catalogmodel.h
QModelIndex
MergeCatalog
Merge source container.
Definition: mergecatalog.h:71
project.h
QWidget
CatalogView::nextEntry
int nextEntry()
Definition: cataloglistview.cpp:273
QToolButton::setMenu
void setMenu(QMenu *menu)
QLayout::setContentsMargins
void setContentsMargins(int left, int top, int right, int bottom)
CatalogTreeFilterModel::individualRejectFilterEnabled
bool individualRejectFilterEnabled()
Definition: catalogmodel.h:145
CatalogTreeModel::headerData
QVariant headerData(int section, Qt::Orientation, int role=Qt::DisplayRole) const
Definition: catalogmodel.cpp:112
QWidget::keyReleaseEvent
virtual void keyReleaseEvent(QKeyEvent *event)
QByteArray
QDockWidget
CatalogView::reset
void reset()
Definition: cataloglistview.cpp:239
QAction::setChecked
void setChecked(bool)
QAction::data
QVariant data() const
Project::instance
static Project * instance()
Definition: project.cpp:67
QMenu::addAction
void addAction(QAction *action)
CatalogView::prevEntry
int prevEntry()
Definition: cataloglistview.cpp:278
CatalogView::CatalogView
CatalogView(QWidget *, Catalog *)
Definition: cataloglistview.cpp:69
QHBoxLayout
CatalogTreeFilterModel::setMergeCatalogPointer
void setMergeCatalogPointer(MergeCatalog *pointer)
Definition: catalogmodel.cpp:302
QString::remove
QString & remove(int position, int n)
CatalogTreeFilterModel::IgnoreAccel
Definition: catalogmodel.h:104
CatalogView::setFocus
void setFocus()
Definition: cataloglistview.cpp:143
QSortFilterProxyModel::rowCount
virtual int rowCount(const QModelIndex &parent) const
DocPosition::entry
int entry
Definition: pos.h:48
CatalogTreeModel::index
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Definition: catalogmodel.cpp:63
DocPosition
This struct represents a position in a catalog.
Definition: pos.h:38
CatalogTreeModel::catalog
Catalog * catalog() const
Definition: catalogmodel.h:75
QMenu::clear
void clear()
CatalogView::setMergeCatalogPointer
void setMergeCatalogPointer(MergeCatalog *pointer)
Definition: cataloglistview.cpp:248
QModelIndex::isValid
bool isValid() const
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QVariant::toInt
int toInt(bool *ok) const
QVariant::isNull
bool isNull() const
CatalogTreeFilterModel::setSourceModel
void setSourceModel(QAbstractItemModel *sourceModel)
Definition: catalogmodel.cpp:220
QWidget::setFocus
void setFocus()
catalog.h
QObject::setObjectName
void setObjectName(const QString &name)
QWidget::setTabOrder
void setTabOrder(QWidget *first, QWidget *second)
QWidget::setFocusProxy
void setFocusProxy(QWidget *w)
CatalogTreeFilterModel::setFilerOptions
void setFilerOptions(int o)
Definition: catalogmodel.cpp:243
QModelIndex::row
int row() const
CatalogTreeModel
MVC wrapper for Catalog.
Definition: catalogmodel.h:41
FIRSTSTATEPOSITION
#define FIRSTSTATEPOSITION
Definition: catalogmodel.h:132
QVBoxLayout
cataloglistview.h
CatalogView::lastEntry
int lastEntry()
Definition: cataloglistview.cpp:296
CatalogTreeFilterModel::setEntriesFilteredOut
void setEntriesFilteredOut(bool filteredOut=false)
Definition: catalogmodel.cpp:227
QShortcut
QMenu::addSeparator
QAction * addSeparator()
QString
CatalogTreeFilterModel::filerOptions
int filerOptions() const
Definition: catalogmodel.h:141
QAction::setData
void setData(const QVariant &userData)
QToolButton
QKeyEvent::key
int key() const
QMenu
QEvent::accept
void accept()
Catalog::capabilities
int capabilities() const
Definition: catalog.cpp:164
CatalogTreeFilterModel
Definition: catalogmodel.h:97
QToolButton::menu
QMenu * menu() const
QAction::setCheckable
void setCheckable(bool)
QDockWidget::setWidget
void setWidget(QWidget *widget)
edgeEntry
static int edgeEntry(CatalogTreeFilterModel *m_proxyModel, int row)
Definition: cataloglistview.cpp:283
QKeyEvent
QSortFilterProxyModel::mapToSource
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const
ExtendedStates
Definition: catalogcapabilities.h:32
QTreeView
QKeySequence
QByteArray::fromBase64
QByteArray fromBase64(const QByteArray &base64)
QSortFilterProxyModel::mapFromSource
virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const
QModelIndex::sibling
QModelIndex sibling(int row, int column) const
CatalogView::slotNewEntryDisplayed
void slotNewEntryDisplayed(const DocPosition &)
Definition: cataloglistview.cpp:149
CatalogView::escaped
void escaped()
QMenu::addMenu
QAction * addMenu(QMenu *menu)
QAction
QWidget::QWidget
QWidget(QWidget *parent, QFlags< Qt::WindowType > f)
QToolButton::setPopupMode
void setPopupMode(ToolButtonPopupMode mode)
CatalogView::gotoEntry
void gotoEntry(const DocPosition &, int selection)
QAbstractButton::setText
void setText(const QString &text)
QAbstractItemView::clicked
void clicked(const QModelIndex &index)
CatalogTreeFilterModel::MaxOption
Definition: catalogmodel.h:127
Catalog
This class represents a catalog It uses CatalogStorage interface to work with catalogs in different f...
Definition: catalog.h:74
CatalogView::setEntryFilteredOut
void setEntryFilteredOut(int entry, bool filteredOut)
Definition: cataloglistview.cpp:302
QWidget::show
void show()
Catalog::states
static const char *const * states()
Definition: catalog.cpp:79
CatalogView::~CatalogView
~CatalogView()
Definition: cataloglistview.cpp:136
QAbstractItemView::currentIndex
QModelIndex currentIndex() const
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
CatalogView::firstEntry
int firstEntry()
Definition: cataloglistview.cpp:291
CatalogTreeFilterModel::New
Definition: catalogmodel.h:117
CatalogTreeFilterModel::AllStates
Definition: catalogmodel.h:128
QVariant::toString
QString toString() const
CatalogTreeModel::DisplayedColumnCount
Definition: catalogmodel.h:57
QSortFilterProxyModel::setFilterKeyColumn
void setFilterKeyColumn(int column)
CatalogView::setEntriesFilteredOut
void setEntriesFilteredOut(bool filteredOut=false)
Definition: cataloglistview.cpp:307
QBoxLayout::setSpacing
void setSpacing(int spacing)
CatalogTreeFilterModel::setEntryFilteredOut
void setEntryFilteredOut(int entry, bool filteredOut)
Definition: catalogmodel.cpp:234
QBoxLayout::addLayout
void addLayout(QLayout *layout, int stretch)
QSortFilterProxyModel::filterRegExp
filterRegExp
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:40:06 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

lokalize

Skip menu "lokalize"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • 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