Changeset View
Changeset View
Standalone View
Standalone View
krusader/BookMan/krbookmarkhandler.cpp
Show All 29 Lines | |||||
30 | #include "../Panel/listpanelactions.h" | 30 | #include "../Panel/listpanelactions.h" | ||
31 | 31 | | |||
32 | // QtCore | 32 | // QtCore | ||
33 | #include <QTextStream> | 33 | #include <QTextStream> | ||
34 | #include <QFile> | 34 | #include <QFile> | ||
35 | #include <QEvent> | 35 | #include <QEvent> | ||
36 | #include <QStandardPaths> | 36 | #include <QStandardPaths> | ||
37 | #include <QDebug> | 37 | #include <QDebug> | ||
38 | #include <QTimer> | ||||
38 | // QtGui | 39 | // QtGui | ||
39 | #include <QMouseEvent> | 40 | #include <QMouseEvent> | ||
40 | #include <QCursor> | 41 | #include <QCursor> | ||
41 | 42 | | |||
42 | #include <KConfigCore/KSharedConfig> | 43 | #include <KConfigCore/KSharedConfig> | ||
43 | #include <KI18n/KLocalizedString> | 44 | #include <KI18n/KLocalizedString> | ||
44 | #include <KIconThemes/KIconLoader> | 45 | #include <KIconThemes/KIconLoader> | ||
45 | #include <KWidgetsAddons/KMessageBox> | 46 | #include <KWidgetsAddons/KMessageBox> | ||
46 | #include <KXmlGui/KActionCollection> | 47 | #include <KXmlGui/KActionCollection> | ||
47 | #include <KBookmarks/KBookmarkManager> | 48 | #include <KBookmarks/KBookmarkManager> | ||
48 | 49 | | |||
49 | #define SPECIAL_BOOKMARKS true | 50 | #define SPECIAL_BOOKMARKS true | ||
50 | 51 | | |||
51 | // ------------------------ for internal use | 52 | // ------------------------ for internal use | ||
52 | #define BOOKMARKS_FILE "krusader/krbookmarks.xml" | 53 | #define BOOKMARKS_FILE "krusader/krbookmarks.xml" | ||
53 | #define CONNECT_BM(X) { disconnect(X, SIGNAL(activated(QUrl)), 0, 0); connect(X, SIGNAL(activated(QUrl)), this, SLOT(slotActivated(QUrl))); } | 54 | #define CONNECT_BM(X) { disconnect(X, SIGNAL(activated(QUrl)), 0, 0); connect(X, SIGNAL(activated(QUrl)), this, SLOT(slotActivated(QUrl))); } | ||
54 | 55 | | |||
55 | KrBookmarkHandler::KrBookmarkHandler(KrMainWindow *mainWindow) : QObject(mainWindow->widget()), | 56 | KrBookmarkHandler::KrBookmarkHandler(KrMainWindow *mainWindow) : | ||
56 | _mainWindow(mainWindow), _middleClick(false), _mainBookmarkPopup(0), _specialBookmarks(), | 57 | QObject(mainWindow->widget()), | ||
57 | _quickSearchAction(nullptr), _quickSearchBar(nullptr) | 58 | _mainWindow(mainWindow), | ||
59 | _middleClick(false), | ||||
60 | _mainBookmarkPopup(0), | ||||
61 | _specialBookmarks(), | ||||
62 | _quickSearchAction(nullptr), | ||||
63 | _quickSearchBar(nullptr), | ||||
64 | _quickSearchMenu(nullptr) | ||||
58 | { | 65 | { | ||
59 | // create our own action collection and make the shortcuts apply only to parent | 66 | // create our own action collection and make the shortcuts apply only to parent | ||
60 | _privateCollection = new KActionCollection(this); | 67 | _privateCollection = new KActionCollection(this); | ||
61 | _collection = _mainWindow->actions(); | 68 | _collection = _mainWindow->actions(); | ||
62 | 69 | | |||
63 | // create _root: father of all bookmarks. it is a dummy bookmark and never shown | 70 | // create _root: father of all bookmarks. it is a dummy bookmark and never shown | ||
64 | _root = new KrBookmark(i18n("Bookmarks")); | 71 | _root = new KrBookmark(i18n("Bookmarks")); | ||
65 | _root->setParent(this); | 72 | _root->setParent(this); | ||
▲ Show 20 Lines • Show All 467 Lines • ▼ Show 20 Line(s) | |||||
533 | 540 | | |||
534 | void KrBookmarkHandler::bookmarksChanged(const QString&, const QString&) | 541 | void KrBookmarkHandler::bookmarksChanged(const QString&, const QString&) | ||
535 | { | 542 | { | ||
536 | importFromFile(); | 543 | importFromFile(); | ||
537 | } | 544 | } | ||
538 | 545 | | |||
539 | bool KrBookmarkHandler::eventFilter(QObject *obj, QEvent *ev) | 546 | bool KrBookmarkHandler::eventFilter(QObject *obj, QEvent *ev) | ||
540 | { | 547 | { | ||
541 | if (obj->inherits("QMenu") && (ev->type() == QEvent::Show || | 548 | auto eventType = ev->type(); | ||
542 | ev->type() == QEvent::Close)) { | 549 | QMenu *menu = dynamic_cast<QMenu *>(obj); | ||
550 | | ||||
551 | if (eventType == QEvent::Show && menu) { | ||||
552 | _setQuickSearchText(""); | ||||
553 | _quickSearchMenu = menu; | ||||
554 | qDebug() << "Bookmark search: menu" << menu << "is shown"; | ||||
555 | } | ||||
martinkostolny: How about placing this line at the end of if statement:
```
return QObject::eventFilter(obj… | |||||
556 | | ||||
557 | if (eventType == QEvent::Close && menu && _quickSearchMenu) { | ||||
558 | if (_quickSearchMenu == menu) { | ||||
559 | qDebug() << "Bookmark search: stopped on menu" << menu; | ||||
543 | _setQuickSearchText(""); | 560 | _setQuickSearchText(""); | ||
561 | _quickSearchMenu = nullptr; | ||||
562 | } | ||||
563 | else { | ||||
Could you explain this? How do we know that this will be scheduled to run after the event that removes focus? rade: Could you explain this? How do we know that this will be scheduled to run after the event that… | |||||
martinkostolny: I'd place `else` to one line with previous closing bracket | |||||
564 | qDebug() << "Bookmark search: active action =" << _quickSearchMenu->activeAction(); | ||||
565 | | ||||
566 | // fix automatic deactivation of current action due to spurious close event from submenu | ||||
567 | auto quickSearchMenu = _quickSearchMenu; | ||||
568 | auto activeAction = _quickSearchMenu->activeAction(); | ||||
569 | QTimer::singleShot(0, this, [=]() { | ||||
570 | qDebug() << "Bookmark search: active action =" << quickSearchMenu->activeAction(); | ||||
571 | if (!quickSearchMenu->activeAction() && activeAction) { | ||||
572 | quickSearchMenu->setActiveAction(activeAction); | ||||
573 | qDebug() << "Bookmark search: restored active action =" << quickSearchMenu->activeAction(); | ||||
574 | } | ||||
575 | }); | ||||
576 | } | ||||
544 | } | 577 | } | ||
martinkostolny: Same here - return statement:
```
return QObject::eventFilter(obj, ev);
``` | |||||
545 | 578 | | |||
546 | // Having it occur on keypress is consistent with other shortcuts, | 579 | // Having it occur on keypress is consistent with other shortcuts, | ||
547 | // such as Ctrl+W and accelerator keys | 580 | // such as Ctrl+W and accelerator keys | ||
548 | if (ev->type() == QEvent::KeyPress && obj->inherits("QMenu")) { | 581 | if (eventType == QEvent::KeyPress && menu) { | ||
549 | QKeyEvent *kev = static_cast<QKeyEvent *>(ev); | 582 | QKeyEvent *kev = static_cast<QKeyEvent *>(ev); | ||
550 | QMenu *menu = static_cast<QMenu *>(obj); | | |||
551 | QList<QAction *> acts = menu->actions(); | 583 | QList<QAction *> acts = menu->actions(); | ||
552 | bool quickSearchStarted = false; | 584 | bool quickSearchStarted = false; | ||
553 | bool searchInSpecialItems = KConfigGroup(krConfig, "Look&Feel").readEntry("Search in special items", false); | 585 | bool searchInSpecialItems = KConfigGroup(krConfig, "Look&Feel").readEntry("Search in special items", false); | ||
554 | 586 | | |||
555 | if (kev->key() == Qt::Key_Left && kev->modifiers() == Qt::NoModifier) { | 587 | if (kev->key() == Qt::Key_Left && kev->modifiers() == Qt::NoModifier) { | ||
556 | menu->close(); | 588 | menu->close(); | ||
557 | return true; | 589 | return true; | ||
558 | } | 590 | } | ||
559 | 591 | | |||
560 | if (kev->modifiers() != Qt::NoModifier || | 592 | if (kev->modifiers() != Qt::NoModifier || | ||
561 | kev->text().isEmpty() || | 593 | kev->text().isEmpty() || | ||
562 | kev->key() == Qt::Key_Delete || | 594 | kev->key() == Qt::Key_Delete || | ||
563 | kev->key() == Qt::Key_Return || | 595 | kev->key() == Qt::Key_Return || | ||
564 | kev->key() == Qt::Key_Escape) | 596 | kev->key() == Qt::Key_Escape) { | ||
565 | 597 | | |||
566 | { | | |||
567 | return QObject::eventFilter(obj, ev); | 598 | return QObject::eventFilter(obj, ev); | ||
568 | } | 599 | } | ||
569 | 600 | | |||
570 | // update quick search text | 601 | // update quick search text | ||
571 | if (kev->key() == Qt::Key_Backspace) { | 602 | if (kev->key() == Qt::Key_Backspace) { | ||
572 | auto newSearchText = _quickSearchText(); | 603 | auto newSearchText = _quickSearchText(); | ||
573 | newSearchText.chop(1); | 604 | newSearchText.chop(1); | ||
574 | _setQuickSearchText(newSearchText); | 605 | _setQuickSearchText(newSearchText); | ||
575 | 606 | | |||
576 | if (_quickSearchText().length() == 0) { | 607 | if (_quickSearchText().length() == 0) { | ||
577 | return QObject::eventFilter(obj, ev); | 608 | return QObject::eventFilter(obj, ev); | ||
578 | } | 609 | } | ||
579 | } else { | 610 | } else { | ||
580 | quickSearchStarted = _quickSearchText().length() == 0; | 611 | quickSearchStarted = _quickSearchText().length() == 0; | ||
581 | _setQuickSearchText(_quickSearchText().append(kev->text())); | 612 | _setQuickSearchText(_quickSearchText().append(kev->text())); | ||
582 | } | 613 | } | ||
583 | 614 | | |||
584 | if (quickSearchStarted) { | 615 | if (quickSearchStarted) { | ||
585 | qDebug() << "Bookmark search: started"; | 616 | _quickSearchMenu = menu; | ||
617 | qDebug() << "Bookmark search: started on menu" << menu; | ||||
586 | } | 618 | } | ||
587 | 619 | | |||
588 | // match actions | 620 | // match actions | ||
589 | QAction *matchedAction = nullptr; | 621 | QAction *matchedAction = nullptr; | ||
590 | int nMatches = 0; | 622 | int nMatches = 0; | ||
591 | for (auto act : acts) { | 623 | for (auto act : acts) { | ||
592 | if (act->isSeparator() || act->text() == "") { | 624 | if (act->isSeparator() || act->text() == "") { | ||
593 | continue; | 625 | continue; | ||
▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Line(s) | 678 | } else { | |||
647 | menu->setActiveAction(nullptr); | 679 | menu->setActiveAction(nullptr); | ||
648 | } | 680 | } | ||
649 | } else { | 681 | } else { | ||
650 | menu->setActiveAction(nullptr); | 682 | menu->setActiveAction(nullptr); | ||
651 | } | 683 | } | ||
652 | return true; | 684 | return true; | ||
653 | } | 685 | } | ||
654 | 686 | | |||
655 | if (ev->type() == QEvent::MouseButtonRelease) { | 687 | if (eventType == QEvent::MouseButtonRelease) { | ||
656 | switch (static_cast<QMouseEvent *>(ev)->button()) { | 688 | switch (static_cast<QMouseEvent *>(ev)->button()) { | ||
657 | case Qt::RightButton: | 689 | case Qt::RightButton: | ||
658 | _middleClick = false; | 690 | _middleClick = false; | ||
659 | if (obj->inherits("QMenu")) { | 691 | if (obj->inherits("QMenu")) { | ||
660 | QMenu *menu = static_cast<QMenu *>(obj); | 692 | QMenu *menu = static_cast<QMenu *>(obj); | ||
661 | QAction *act = menu->actionAt(static_cast<QMouseEvent *>(ev)->pos()); | 693 | QAction *act = menu->actionAt(static_cast<QMouseEvent *>(ev)->pos()); | ||
662 | 694 | | |||
663 | if (obj == _mainBookmarkPopup && _specialBookmarks.contains(act)) { | 695 | if (obj == _mainBookmarkPopup && _specialBookmarks.contains(act)) { | ||
▲ Show 20 Lines • Show All 175 Lines • Show Last 20 Lines |
How about placing this line at the end of if statement:
IMO it would improve readability and also it would be a minor optimization in some cases.