Krita Source Code Documentation
Loading...
Searching...
No Matches
KateCommandBar Class Reference

#include <katecommandbar.h>

+ Inheritance diagram for KateCommandBar:

Public Member Functions

 KateCommandBar (QWidget *parent=nullptr)
 
void updateBar (const QList< KisKActionCollection * > &actions, int totalActions)
 
void updateViewGeometry ()
 

Protected Member Functions

bool eventFilter (QObject *obj, QEvent *event) override
 

Private Slots

void reselectFirst ()
 
void slotReturnPressed ()
 

Private Attributes

QVector< KisKActionCollection * > m_disposableActionCollections
 
QLineEdit * m_lineEdit
 
CommandModelm_model
 
CommandBarFilterModelm_proxyModel
 
QTreeView * m_treeView
 

Detailed Description

Definition at line 18 of file katecommandbar.h.

Constructor & Destructor Documentation

◆ KateCommandBar()

KateCommandBar::KateCommandBar ( QWidget * parent = nullptr)

Definition at line 241 of file katecommandbar.cpp.

242 : QMenu(parent)
243{
244 QVBoxLayout *layout = new QVBoxLayout();
245 layout->setSpacing(0);
246 layout->setContentsMargins(4, 4, 4, 4);
247 setLayout(layout);
248
249 m_lineEdit = new QLineEdit(this);
250 setFocusProxy(m_lineEdit);
251
252 layout->addWidget(m_lineEdit);
253
254 m_treeView = new QTreeView();
255 layout->addWidget(m_treeView, 1);
256 m_treeView->setTextElideMode(Qt::ElideMiddle);
257 m_treeView->setUniformRowHeights(true);
258
259 m_model = new CommandModel(this);
260
263 m_treeView->setItemDelegateForColumn(0, delegate);
264 m_treeView->setItemDelegateForColumn(1, del);
265
267 m_proxyModel->setFilterRole(Qt::DisplayRole);
268 m_proxyModel->setSortRole(CommandModel::Score);
269 m_proxyModel->setFilterKeyColumn(0);
270
271 connect(m_lineEdit, &QLineEdit::returnPressed, this, &KateCommandBar::slotReturnPressed);
273 connect(m_lineEdit, &QLineEdit::textChanged, delegate, &CommandBarStyleDelegate::setFilterString);
274 connect(m_lineEdit, &QLineEdit::textChanged, this, [this]() {
275 m_treeView->viewport()->update();
277 });
278 connect(m_treeView, &QTreeView::clicked, this, &KateCommandBar::slotReturnPressed);
279
280 m_proxyModel->setSourceModel(m_model);
281 m_treeView->setSortingEnabled(true);
282 m_treeView->setModel(m_proxyModel);
283
284 m_treeView->installEventFilter(this);
285 m_lineEdit->installEventFilter(this);
286
287 m_treeView->setHeaderHidden(true);
288 m_treeView->setRootIsDecorated(false);
289 m_treeView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
290 m_treeView->setSelectionMode(QTreeView::SingleSelection);
291
292 setHidden(true);
293}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
Q_SLOT void setFilterString(const QString &string)
void setFilterString(const QString &text)
CommandModel * m_model
QTreeView * m_treeView
CommandBarFilterModel * m_proxyModel
QLineEdit * m_lineEdit
bool update(QSpinBox *spinBox)

References connect(), m_lineEdit, m_model, m_proxyModel, m_treeView, reselectFirst(), CommandModel::Score, CommandBarFilterModel::setFilterString(), CommandBarStyleDelegate::setFilterString(), and slotReturnPressed().

Member Function Documentation

◆ eventFilter()

bool KateCommandBar::eventFilter ( QObject * obj,
QEvent * event )
overrideprotected

Definition at line 331 of file katecommandbar.cpp.

332{
333 // catch key presses + shortcut overrides to allow to have ESC as application wide shortcut, too, see bug 409856
334 if (event->type() == QEvent::KeyPress || event->type() == QEvent::ShortcutOverride) {
335 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
336 if (obj == m_lineEdit) {
337 const bool forward2list = (keyEvent->key() == Qt::Key_Up) || (keyEvent->key() == Qt::Key_Down) || (keyEvent->key() == Qt::Key_PageUp)
338 || (keyEvent->key() == Qt::Key_PageDown);
339 if (forward2list) {
340 QCoreApplication::sendEvent(m_treeView, event);
341 return true;
342 }
343
344 if (keyEvent->key() == Qt::Key_Escape) {
345 m_lineEdit->clear();
346 keyEvent->accept();
347 hide();
348 return true;
349 }
350 } else {
351 const bool forward2input = (keyEvent->key() != Qt::Key_Up) && (keyEvent->key() != Qt::Key_Down) && (keyEvent->key() != Qt::Key_PageUp)
352 && (keyEvent->key() != Qt::Key_PageDown) && (keyEvent->key() != Qt::Key_Tab) && (keyEvent->key() != Qt::Key_Backtab);
353 if (forward2input) {
354 QCoreApplication::sendEvent(m_lineEdit, event);
355 return true;
356 }
357 }
358 }
359
360 // hide on focus out, if neither input field nor list have focus!
361 else if (event->type() == QEvent::FocusOut && !(m_lineEdit->hasFocus() || m_treeView->hasFocus())) {
362 m_lineEdit->clear();
363 hide();
364 return true;
365 }
366
367 return QWidget::eventFilter(obj, event);
368}

References m_lineEdit, and m_treeView.

◆ reselectFirst

void KateCommandBar::reselectFirst ( )
privateslot

Definition at line 404 of file katecommandbar.cpp.

405{
406 QModelIndex index = m_proxyModel->index(0, 0);
407 m_treeView->setCurrentIndex(index);
408}

References m_proxyModel, and m_treeView.

◆ slotReturnPressed

void KateCommandBar::slotReturnPressed ( )
privateslot

Definition at line 370 of file katecommandbar.cpp.

371{
372 auto act = m_proxyModel->data(m_treeView->currentIndex(), Qt::UserRole).value<QAction *>();
373 if (act) {
374 // if the action is a menu, we take all its actions
375 // and reload our dialog with these instead.
376 if (auto menu = act->menu()) {
377 auto menuActions = menu->actions();
379 list.reserve(menuActions.size());
380
381 // if there are no actions, trigger load actions
382 // this happens with some menus that are loaded on demand
383 if (menuActions.size() == 0) {
384 Q_EMIT menu->aboutToShow();
385 menuActions = menu->actions();
386 }
387
388 for (auto menuAction : std::as_const(menuActions)) {
389 if (menuAction) {
390 list.append({KLocalizedString::removeAcceleratorMarker(act->text()), menuAction});
391 }
392 }
393 m_model->refresh(list);
394 m_lineEdit->clear();
395 return;
396 } else {
397 act->trigger();
398 }
399 }
400 m_lineEdit->clear();
401 hide();
402}
void refresh(QVector< QPair< QString, QAction * > > actionList)

References m_lineEdit, m_model, m_proxyModel, m_treeView, and CommandModel::refresh().

◆ updateBar()

void KateCommandBar::updateBar ( const QList< KisKActionCollection * > & actions,
int totalActions )

Definition at line 295 of file katecommandbar.cpp.

296{
299
301 actionList.reserve(totalActions);
302
303 for (const auto collection : actionCollections) {
304
305 if (collection->componentName().contains("disposable")) {
306 m_disposableActionCollections << collection;
307 }
308
309 const QList<QAction *> collectionActions = collection->actions();
310 const QString componentName = collection->componentDisplayName();
311 for (const auto action : collectionActions) {
312 // sanity + empty check ensures displayable actions and removes ourself
313 // from the action list
314 if (action && action->isEnabled() && !action->text().isEmpty()) {
315 actionList.append({componentName, action});
316 }
317 }
318 }
319
320
321
322
323 m_model->refresh(std::move(actionList));
325
327 show();
328 setFocus();
329}
QVector< KisKActionCollection * > m_disposableActionCollections

References m_disposableActionCollections, m_model, CommandModel::refresh(), reselectFirst(), and updateViewGeometry().

◆ updateViewGeometry()

void KateCommandBar::updateViewGeometry ( )

Definition at line 410 of file katecommandbar.cpp.

411{
412 m_treeView->resizeColumnToContents(0);
413 m_treeView->resizeColumnToContents(1);
414
415 const QSize centralSize = parentWidget()->size();
416
417 // width: 2.4 of editor, height: 1/2 of editor
418 const QSize viewMaxSize(centralSize.width() / 2.4, centralSize.height() / 2);
419
420 // Position should be central over window
421 const int xPos = std::max(0, (centralSize.width() - viewMaxSize.width()) / 2);
422 const int yPos = std::max(0, (centralSize.height() - viewMaxSize.height()) * 1 / 4);
423
424 const QPoint p(xPos, yPos);
425 move(p + parentWidget()->pos());
426
427 this->setFixedSize(viewMaxSize);
428}
const Params2D p

References m_treeView, and p.

Member Data Documentation

◆ m_disposableActionCollections

QVector<KisKActionCollection *> KateCommandBar::m_disposableActionCollections
private

Definition at line 40 of file katecommandbar.h.

◆ m_lineEdit

QLineEdit* KateCommandBar::m_lineEdit
private

Definition at line 37 of file katecommandbar.h.

◆ m_model

CommandModel* KateCommandBar::m_model
private

Definition at line 38 of file katecommandbar.h.

◆ m_proxyModel

CommandBarFilterModel* KateCommandBar::m_proxyModel
private

Definition at line 39 of file katecommandbar.h.

◆ m_treeView

QTreeView* KateCommandBar::m_treeView
private

Definition at line 36 of file katecommandbar.h.


The documentation for this class was generated from the following files: