| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- #include "vopenedlistmenu.h"
- #include <QActionGroup>
- #include <QAction>
- #include <QFont>
- #include <QVector>
- #include <QTimer>
- #include <QString>
- #include <QStyleFactory>
- #include "veditwindow.h"
- #include "vnotefile.h"
- #include "vedittab.h"
- #include "vdirectory.h"
- #include "utils/vutils.h"
- static const int c_cmdTime = 1 * 1000;
- static bool fileComp(const VOpenedListMenu::ItemInfo &a,
- const VOpenedListMenu::ItemInfo &b)
- {
- QString notebooka, notebookb;
- if (a.file->getType() == FileType::Note) {
- notebooka = dynamic_cast<const VNoteFile *>(a.file)->getNotebookName().toLower();
- } else {
- notebooka = "EXTERNAL_FILES";
- }
- if (b.file->getType() == FileType::Note) {
- notebookb = dynamic_cast<const VNoteFile *>(b.file)->getNotebookName().toLower();
- } else {
- notebookb = "EXTERNAL_FILES";
- }
- if (notebooka < notebookb) {
- return true;
- } else if (notebooka > notebookb) {
- return false;
- } else {
- QString patha = a.file->fetchBasePath();
- QString pathb = b.file->fetchBasePath();
- #if defined(Q_OS_WIN)
- patha = patha.toLower();
- pathb = pathb.toLower();
- #endif
- if (patha == pathb) {
- return a.index < b.index;
- } else {
- return patha < pathb;
- }
- }
- }
- VOpenedListMenu::VOpenedListMenu(VEditWindow *p_editWin)
- : QMenu(p_editWin), m_editWin(p_editWin), m_cmdNum(0)
- {
- // Force to display separator text on Windows and macOS.
- setStyle(QStyleFactory::create("Fusion"));
- int separatorHeight = 20 * VUtils::calculateScaleFactor();
- QString style = QString("::separator { color: #009688; height: %1px; padding-top: 3px; }").arg(separatorHeight);
- setStyleSheet(style);
- setToolTipsVisible(true);
- m_cmdTimer = new QTimer(this);
- m_cmdTimer->setSingleShot(true);
- m_cmdTimer->setInterval(c_cmdTime);
- connect(m_cmdTimer, &QTimer::timeout,
- this, &VOpenedListMenu::cmdTimerTimeout);
- connect(this, &QMenu::aboutToShow,
- this, &VOpenedListMenu::updateOpenedList);
- connect(this, &QMenu::triggered,
- this, &VOpenedListMenu::handleItemTriggered);
- }
- void VOpenedListMenu::updateOpenedList()
- {
- // Regenerate the opened list.
- m_seqActionMap.clear();
- clear();
- int curTab = m_editWin->currentIndex();
- int nrTab = m_editWin->count();
- QVector<ItemInfo> files(nrTab);
- for (int i = 0; i < nrTab; ++i) {
- files[i].file = m_editWin->getTab(i)->getFile();
- files[i].index = i;
- }
- std::sort(files.begin(), files.end(), fileComp);
- QString notebook;
- const VDirectory *directory = NULL;
- QFont sepFont;
- sepFont.setItalic(true);
- for (int i = 0; i < nrTab; ++i) {
- QPointer<VFile> file = files[i].file;
- int index = files[i].index;
- // Whether add separator.
- QString curNotebook;
- const VDirectory *curDirectory = NULL;
- if (file->getType() == FileType::Note) {
- const VNoteFile *tmpFile = dynamic_cast<const VNoteFile *>((VFile *)file);
- curNotebook = tmpFile->getNotebookName();
- curDirectory = tmpFile->getDirectory();
- } else {
- curNotebook = "EXTERNAL_FILES";
- }
- if (curNotebook != notebook
- || curDirectory != directory) {
- notebook = curNotebook;
- directory = curDirectory;
- QString dirName;
- if (directory) {
- dirName = directory->getName();
- }
- QString text;
- if (dirName.isEmpty()) {
- text = QString("[%1]").arg(notebook);
- } else {
- text = QString("[%1] %2").arg(notebook).arg(dirName);
- }
- QAction *sepAct = addSection(text);
- sepAct->setFont(sepFont);
- }
- QAction *action = new QAction(m_editWin->tabIcon(index),
- m_editWin->tabText(index));
- action->setToolTip(generateDescription(file));
- action->setData(QVariant::fromValue(file));
- if (index == curTab) {
- QFont boldFont;
- boldFont.setBold(true);
- action->setFont(boldFont);
- }
- addAction(action);
- m_seqActionMap[index + c_tabSequenceBase] = action;
- }
- }
- QString VOpenedListMenu::generateDescription(const VFile *p_file) const
- {
- if (!p_file) {
- return "";
- }
- // [Notebook]path
- if (p_file->getType() == FileType::Note) {
- const VNoteFile *tmpFile = dynamic_cast<const VNoteFile *>(p_file);
- return QString("[%1] %2").arg(tmpFile->getNotebookName()).arg(tmpFile->fetchPath());
- } else {
- return QString("%1").arg(p_file->fetchPath());
- }
- }
- void VOpenedListMenu::handleItemTriggered(QAction *p_action)
- {
- if (!p_action) {
- return;
- }
- QPointer<VFile> file = p_action->data().value<QPointer<VFile>>();
- emit fileTriggered(file);
- }
- void VOpenedListMenu::keyPressEvent(QKeyEvent *p_event)
- {
- int key = p_event->key();
- int modifiers = p_event->modifiers();
- switch (key) {
- case Qt::Key_0:
- case Qt::Key_1:
- case Qt::Key_2:
- case Qt::Key_3:
- case Qt::Key_4:
- case Qt::Key_5:
- case Qt::Key_6:
- case Qt::Key_7:
- case Qt::Key_8:
- case Qt::Key_9:
- {
- addDigit(key - Qt::Key_0);
- return;
- }
- case Qt::Key_BracketLeft:
- {
- m_cmdTimer->stop();
- m_cmdNum = 0;
- if (modifiers == Qt::ControlModifier) {
- hide();
- return;
- }
- break;
- }
- case Qt::Key_J:
- {
- m_cmdTimer->stop();
- m_cmdNum = 0;
- if (modifiers == Qt::ControlModifier) {
- QList<QAction *> acts = actions();
- if (acts.size() == 0) {
- return;
- }
- int idx = 0;
- QAction *act = activeAction();
- if (act) {
- for (int i = 0; i < acts.size(); ++i) {
- if (acts.at(i) == act) {
- idx = i + 1;
- break;
- }
- }
- }
- while (true) {
- if (idx >= acts.size()) {
- idx = 0;
- }
- act = acts.at(idx);
- if (act->isSeparator() || !act->isVisible()) {
- ++idx;
- } else {
- break;
- }
- }
- setActiveAction(act);
- return;
- }
- break;
- }
- case Qt::Key_K:
- {
- m_cmdTimer->stop();
- m_cmdNum = 0;
- if (modifiers == Qt::ControlModifier) {
- QList<QAction *> acts = actions();
- if (acts.size() == 0) {
- return;
- }
- int idx = acts.size() - 1;
- QAction *act = activeAction();
- if (act) {
- for (int i = 0; i < acts.size(); ++i) {
- if (acts.at(i) == act) {
- idx = i - 1;
- break;
- }
- }
- }
- while (true) {
- if (idx < 0) {
- idx = acts.size() - 1;
- }
- act = acts.at(idx);
- if (act->isSeparator() || !act->isVisible()) {
- --idx;
- } else {
- break;
- }
- }
- setActiveAction(act);
- return;
- }
- break;
- }
- default:
- m_cmdTimer->stop();
- m_cmdNum = 0;
- break;
- }
- QMenu::keyPressEvent(p_event);
- }
- void VOpenedListMenu::cmdTimerTimeout()
- {
- if (m_cmdNum > 0) {
- triggerItem(m_cmdNum);
- m_cmdNum = 0;
- }
- }
- void VOpenedListMenu::addDigit(int p_digit)
- {
- V_ASSERT(p_digit >= 0 && p_digit <= 9);
- m_cmdTimer->stop();
- m_cmdNum = m_cmdNum * 10 + p_digit;
- int totalItem = m_seqActionMap.size();
- // Try to trigger it ASAP.
- if (m_cmdNum > 0) {
- if (getNumOfDigit(m_cmdNum) == getNumOfDigit(totalItem)) {
- triggerItem(m_cmdNum);
- m_cmdNum = 0;
- return;
- }
- // Set active action to the candidate.
- auto it = m_seqActionMap.find(m_cmdNum);
- if (it != m_seqActionMap.end()) {
- QAction *act = it.value();
- setActiveAction(act);
- }
- }
- m_cmdTimer->start();
- }
- int VOpenedListMenu::getNumOfDigit(int p_num)
- {
- int nrDigit = 1;
- while (true) {
- p_num /= 10;
- if (p_num == 0) {
- return nrDigit;
- } else {
- ++nrDigit;
- }
- }
- }
- void VOpenedListMenu::triggerItem(int p_seq)
- {
- auto it = m_seqActionMap.find(p_seq);
- if (it != m_seqActionMap.end()) {
- QAction *act = it.value();
- act->trigger();
- hide();
- }
- }
|