| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324 |
- #include "veditarea.h"
- #include <QtWidgets>
- #include "veditwindow.h"
- #include "vedittab.h"
- #include "vnote.h"
- #include "vconfigmanager.h"
- #include "vfile.h"
- #include "dialog/vfindreplacedialog.h"
- #include "utils/vutils.h"
- #include "vfilesessioninfo.h"
- #include "vmainwindow.h"
- #include "vcaptain.h"
- #include "vfilelist.h"
- #include "vmathjaxpreviewhelper.h"
- #include "vmdtab.h"
- #include "vmdeditor.h"
- extern VConfigManager *g_config;
- extern VNote *g_vnote;
- extern VMainWindow *g_mainWin;
- VEditArea::VEditArea(QWidget *parent)
- : QWidget(parent),
- VNavigationMode(),
- curWindowIndex(-1)
- {
- setupUI();
- insertSplitWindow(0);
- setCurrentWindow(0, false);
- registerCaptainTargets();
- initShortcuts();
- QTimer *timer = new QTimer(this);
- timer->setSingleShot(false);
- timer->setInterval(g_config->getFileTimerInterval());
- connect(timer, &QTimer::timeout,
- this, &VEditArea::handleFileTimerTimeout);
- timer->start();
- m_autoSave = g_config->getEnableAutoSave();
- m_mathPreviewHelper = new VMathJaxPreviewHelper(this, this);
- }
- void VEditArea::setupUI()
- {
- splitter = new QSplitter(this);
- m_findReplace = new VFindReplaceDialog(this);
- m_findReplace->setOption(FindOption::CaseSensitive,
- g_config->getFindCaseSensitive());
- m_findReplace->setOption(FindOption::WholeWordOnly,
- g_config->getFindWholeWordOnly());
- m_findReplace->setOption(FindOption::RegularExpression,
- g_config->getFindRegularExpression());
- m_findReplace->setOption(FindOption::IncrementalSearch,
- g_config->getFindIncrementalSearch());
- QVBoxLayout *mainLayout = new QVBoxLayout();
- mainLayout->addWidget(splitter);
- mainLayout->addWidget(m_findReplace);
- mainLayout->setContentsMargins(0, 0, 0, 0);
- mainLayout->setSpacing(0);
- mainLayout->setStretch(0, 1);
- mainLayout->setStretch(1, 0);
- setLayout(mainLayout);
- connect(m_findReplace, &VFindReplaceDialog::findTextChanged,
- this, &VEditArea::handleFindTextChanged);
- connect(m_findReplace, &VFindReplaceDialog::findOptionChanged,
- this, &VEditArea::handleFindOptionChanged);
- connect(m_findReplace, SIGNAL(findNext(const QString &, uint, bool)),
- this, SLOT(handleFindNext(const QString &, uint, bool)));
- connect(m_findReplace,
- SIGNAL(replace(const QString &, uint, const QString &, bool)),
- this,
- SLOT(handleReplace(const QString &, uint, const QString &, bool)));
- connect(m_findReplace,
- SIGNAL(replaceAll(const QString &, uint, const QString &)),
- this,
- SLOT(handleReplaceAll(const QString &, uint, const QString &)));
- connect(m_findReplace, &VFindReplaceDialog::dialogClosed,
- this, &VEditArea::handleFindDialogClosed);
- m_findReplace->hide();
- // Shortcut Ctrl+Shift+T to open last closed file.
- QString keySeq = g_config->getShortcutKeySequence("LastClosedFile");
- qDebug() << "set LastClosedFile shortcut to" << keySeq;
- QShortcut *lastClosedFileShortcut = new QShortcut(QKeySequence(keySeq), this);
- lastClosedFileShortcut->setContext(Qt::ApplicationShortcut);
- connect(lastClosedFileShortcut, &QShortcut::activated,
- this, [this]() {
- if (!m_lastClosedFiles.isEmpty()) {
- const VFileSessionInfo &file = m_lastClosedFiles.top();
- QVector<VFileSessionInfo> files(1, file);
- m_lastClosedFiles.pop();
- openFiles(files);
- }
- });
- }
- void VEditArea::initShortcuts()
- {
- QString keySeq = g_config->getShortcutKeySequence("ActivateNextTab");
- qDebug() << "set ActivateNextTab shortcut to" << keySeq;
- QShortcut *activateNextTab = new QShortcut(QKeySequence(keySeq), this);
- activateNextTab->setContext(Qt::ApplicationShortcut);
- connect(activateNextTab, &QShortcut::activated,
- this, [this]() {
- VEditWindow *win = getCurrentWindow();
- if (win) {
- win->focusNextTab(true);
- }
- });
- keySeq = g_config->getShortcutKeySequence("ActivatePreviousTab");
- qDebug() << "set ActivatePreviousTab shortcut to" << keySeq;
- QShortcut *activatePreviousTab = new QShortcut(QKeySequence(keySeq), this);
- activatePreviousTab->setContext(Qt::ApplicationShortcut);
- connect(activatePreviousTab, &QShortcut::activated,
- this, [this]() {
- VEditWindow *win = getCurrentWindow();
- if (win) {
- win->focusNextTab(false);
- }
- });
- keySeq = g_config->getShortcutKeySequence("NextMatch");
- qDebug() << "set NextMatch shortcut to" << keySeq;
- QShortcut *nextMatchSC = new QShortcut(QKeySequence(keySeq), this);
- connect(nextMatchSC, &QShortcut::activated,
- this, [this]() {
- nextMatch(true);
- });
- keySeq = g_config->getShortcutKeySequence("PreviousMatch");
- qDebug() << "set PreviousMatch shortcut to" << keySeq;
- QShortcut *previousMatchSC = new QShortcut(QKeySequence(keySeq), this);
- connect(previousMatchSC, &QShortcut::activated,
- this, [this]() {
- nextMatch(false);
- });
- }
- void VEditArea::insertSplitWindow(int idx)
- {
- VEditWindow *win = new VEditWindow(this);
- splitter->insertWidget(idx, win);
- connect(win, &VEditWindow::tabStatusUpdated,
- this, &VEditArea::handleWindowTabStatusUpdated);
- connect(win, &VEditWindow::requestSplitWindow,
- this, &VEditArea::splitWindow);
- connect(win, &VEditWindow::requestRemoveSplit,
- this, &VEditArea::handleRemoveSplitRequest);
- connect(win, &VEditWindow::getFocused,
- this, &VEditArea::handleWindowFocused);
- connect(win, &VEditWindow::outlineChanged,
- this, &VEditArea::handleWindowOutlineChanged);
- connect(win, &VEditWindow::currentHeaderChanged,
- this, &VEditArea::handleWindowCurrentHeaderChanged);
- connect(win, &VEditWindow::statusMessage,
- this, &VEditArea::handleWindowStatusMessage);
- connect(win, &VEditWindow::vimStatusUpdated,
- this, &VEditArea::handleWindowVimStatusUpdated);
- }
- void VEditArea::handleWindowTabStatusUpdated(const VEditTabInfo &p_info)
- {
- if (splitter->widget(curWindowIndex) == sender()) {
- emit tabStatusUpdated(p_info);
- }
- }
- void VEditArea::handleWindowStatusMessage(const QString &p_msg)
- {
- if (splitter->widget(curWindowIndex) == sender()) {
- emit statusMessage(p_msg);
- }
- }
- void VEditArea::handleWindowVimStatusUpdated(const VVim *p_vim)
- {
- if (splitter->widget(curWindowIndex) == sender()) {
- emit vimStatusUpdated(p_vim);
- }
- }
- void VEditArea::removeSplitWindow(VEditWindow *win)
- {
- if (!win) {
- return;
- }
- win->hide();
- win->setParent(this);
- disconnect(win, 0, this, 0);
- // Should be deleted later
- win->deleteLater();
- }
- VEditTab *VEditArea::openFile(VFile *p_file, OpenFileMode p_mode, bool p_forceMode)
- {
- if (!p_file) {
- return NULL;
- }
- // Update auto save settings.
- m_autoSave = g_config->getEnableAutoSave();
- // If it is DocType::Unknown, open it using system default method.
- if (p_file->getDocType() == DocType::Unknown) {
- QUrl url = QUrl::fromLocalFile(p_file->fetchPath());
- QDesktopServices::openUrl(url);
- return NULL;
- }
- // Find if it has been opened already
- int winIdx, tabIdx;
- bool existFile = false;
- bool setFocus = false;
- auto tabs = findTabsByFile(p_file);
- if (!tabs.empty()) {
- // Current window first
- winIdx = tabs[0].first;
- tabIdx = tabs[0].second;
- for (int i = 0; i < tabs.size(); ++i) {
- if (tabs[i].first == curWindowIndex) {
- winIdx = tabs[i].first;
- tabIdx = tabs[i].second;
- break;
- }
- }
- setFocus = true;
- existFile = true;
- goto out;
- }
- // Open it in current window
- if (curWindowIndex == -1) {
- // insert a new split
- insertSplitWindow(0);
- curWindowIndex = 0;
- }
- winIdx = curWindowIndex;
- tabIdx = openFileInWindow(winIdx, p_file, p_mode);
- out:
- VEditTab *tab = getTab(winIdx, tabIdx);
- setCurrentTab(winIdx, tabIdx, setFocus);
- if (existFile && p_forceMode) {
- if (p_mode == OpenFileMode::Read) {
- readFile();
- } else if (p_mode == OpenFileMode::Edit) {
- editFile();
- }
- }
- return tab;
- }
- QVector<QPair<int, int> > VEditArea::findTabsByFile(const VFile *p_file)
- {
- QVector<QPair<int, int> > tabs;
- int nrWin = splitter->count();
- for (int winIdx = 0; winIdx < nrWin; ++winIdx) {
- VEditWindow *win = getWindow(winIdx);
- int tabIdx = win->findTabByFile(p_file);
- if (tabIdx != -1) {
- QPair<int, int> match;
- match.first = winIdx;
- match.second = tabIdx;
- tabs.append(match);
- }
- }
- return tabs;
- }
- int VEditArea::openFileInWindow(int windowIndex, VFile *p_file, OpenFileMode p_mode)
- {
- Q_ASSERT(windowIndex < splitter->count());
- VEditWindow *win = getWindow(windowIndex);
- return win->openFile(p_file, p_mode);
- }
- void VEditArea::setCurrentTab(int windowIndex, int tabIndex, bool setFocus)
- {
- VEditWindow *win = getWindow(windowIndex);
- win->setCurrentIndex(tabIndex);
- setCurrentWindow(windowIndex, setFocus);
- }
- void VEditArea::setCurrentWindow(int windowIndex, bool setFocus)
- {
- int nrWin = splitter->count();
- curWindowIndex = windowIndex;
- for (int i = 0; i < nrWin; ++i) {
- getWindow(i)->setCurrentWindow(false);
- }
- if (curWindowIndex > -1) {
- getWindow(curWindowIndex)->setCurrentWindow(true);
- if (setFocus) {
- getWindow(curWindowIndex)->focusWindow();
- }
- }
- // Update status
- updateWindowStatus();
- }
- void VEditArea::updateWindowStatus()
- {
- if (curWindowIndex == -1) {
- Q_ASSERT(splitter->count() == 0);
- emit tabStatusUpdated(VEditTabInfo());
- emit outlineChanged(VTableOfContent());
- emit currentHeaderChanged(VHeaderPointer());
- return;
- }
- VEditWindow *win = getWindow(curWindowIndex);
- win->updateTabStatus();
- }
- bool VEditArea::closeFile(const VFile *p_file, bool p_forced)
- {
- if (!p_file) {
- return true;
- }
- bool ret = false;
- int i = 0;
- while (i < splitter->count()) {
- VEditWindow *win = getWindow(i);
- int nrWin = splitter->count();
- ret = ret || win->closeFile(p_file, p_forced);
- if (nrWin == splitter->count()) {
- ++i;
- }
- }
- updateWindowStatus();
- return ret;
- }
- bool VEditArea::closeFile(const VDirectory *p_dir, bool p_forced)
- {
- if (!p_dir) {
- return true;
- }
- int i = 0;
- while (i < splitter->count()) {
- VEditWindow *win = getWindow(i);
- if (!p_forced) {
- setCurrentWindow(i, false);
- }
- int nrWin = splitter->count();
- if (!win->closeFile(p_dir, p_forced)) {
- return false;
- }
- // win may be removed after closeFile()
- if (nrWin == splitter->count()) {
- ++i;
- }
- }
- updateWindowStatus();
- return true;
- }
- bool VEditArea::closeFile(const VNotebook *p_notebook, bool p_forced)
- {
- if (!p_notebook) {
- return true;
- }
- int i = 0;
- while (i < splitter->count()) {
- VEditWindow *win = getWindow(i);
- if (!p_forced) {
- setCurrentWindow(i, false);
- }
- int nrWin = splitter->count();
- if (!win->closeFile(p_notebook, p_forced)) {
- return false;
- }
- // win may be removed after closeFile()
- if (nrWin == splitter->count()) {
- ++i;
- }
- }
- updateWindowStatus();
- return true;
- }
- bool VEditArea::closeAllFiles(bool p_forced)
- {
- int i = 0;
- while (i < splitter->count()) {
- VEditWindow *win = getWindow(i);
- if (!p_forced) {
- setCurrentWindow(i, false);
- }
- int nrWin = splitter->count();
- if (!win->closeAllFiles(p_forced)) {
- return false;
- }
- if (nrWin == splitter->count()) {
- ++i;
- }
- }
- updateWindowStatus();
- return true;
- }
- void VEditArea::editFile()
- {
- VEditWindow *win = getWindow(curWindowIndex);
- win->editFile();
- }
- void VEditArea::saveFile()
- {
- VEditWindow *win = getWindow(curWindowIndex);
- win->saveFile();
- }
- void VEditArea::readFile(bool p_discard)
- {
- VEditWindow *win = getWindow(curWindowIndex);
- win->readFile(p_discard);
- }
- void VEditArea::saveAndReadFile()
- {
- VEditWindow *win = getWindow(curWindowIndex);
- win->saveAndReadFile();
- }
- void VEditArea::splitWindow(VEditWindow *p_window, bool p_right)
- {
- if (!p_window) {
- return;
- }
- int idx = splitter->indexOf(p_window);
- Q_ASSERT(idx > -1);
- if (p_right) {
- ++idx;
- } else {
- --idx;
- if (idx < 0) {
- idx = 0;
- }
- }
- insertSplitWindow(idx);
- setCurrentWindow(idx, true);
- distributeSplits();
- }
- void VEditArea::splitCurrentWindow()
- {
- if (curWindowIndex > -1) {
- splitWindow(getWindow(curWindowIndex));
- }
- }
- void VEditArea::handleRemoveSplitRequest(VEditWindow *curWindow)
- {
- if (!curWindow || splitter->count() <= 1) {
- return;
- }
- int idx = splitter->indexOf(curWindow);
- // curWindow will be deleted later
- removeSplitWindow(curWindow);
- if (idx >= splitter->count()) {
- idx = splitter->count() - 1;
- }
- setCurrentWindow(idx, true);
- }
- void VEditArea::removeCurrentWindow()
- {
- if (curWindowIndex > -1) {
- handleRemoveSplitRequest(getWindow(curWindowIndex));
- }
- }
- void VEditArea::mousePressEvent(QMouseEvent *event)
- {
- QPoint pos = event->pos();
- int nrWin = splitter->count();
- for (int i = 0; i < nrWin; ++i) {
- VEditWindow *win = getWindow(i);
- if (win->geometry().contains(pos, true)) {
- setCurrentWindow(i, true);
- break;
- }
- }
- QWidget::mousePressEvent(event);
- }
- void VEditArea::handleWindowFocused()
- {
- QObject *winObject = sender();
- int nrWin = splitter->count();
- for (int i = 0; i < nrWin; ++i) {
- if (splitter->widget(i) == winObject) {
- setCurrentWindow(i, false);
- break;
- }
- }
- }
- void VEditArea::handleWindowOutlineChanged(const VTableOfContent &p_outline)
- {
- QObject *winObject = sender();
- if (splitter->widget(curWindowIndex) == winObject) {
- emit outlineChanged(p_outline);
- }
- }
- void VEditArea::handleWindowCurrentHeaderChanged(const VHeaderPointer &p_header)
- {
- QObject *winObject = sender();
- if (splitter->widget(curWindowIndex) == winObject) {
- emit currentHeaderChanged(p_header);
- }
- }
- void VEditArea::scrollToHeader(const VHeaderPointer &p_header)
- {
- VEditWindow *win = getCurrentWindow();
- if (win) {
- win->scrollToHeader(p_header);
- }
- }
- bool VEditArea::isFileOpened(const VFile *p_file)
- {
- return !findTabsByFile(p_file).isEmpty();
- }
- void VEditArea::handleFileUpdated(const VFile *p_file, UpdateAction p_act)
- {
- int nrWin = splitter->count();
- for (int i = 0; i < nrWin; ++i) {
- getWindow(i)->updateFileInfo(p_file, p_act);
- }
- }
- void VEditArea::handleDirectoryUpdated(const VDirectory *p_dir, UpdateAction p_act)
- {
- int nrWin = splitter->count();
- for (int i = 0; i < nrWin; ++i) {
- getWindow(i)->updateDirectoryInfo(p_dir, p_act);
- }
- }
- void VEditArea::handleNotebookUpdated(const VNotebook *p_notebook)
- {
- int nrWin = splitter->count();
- for (int i = 0; i < nrWin; ++i) {
- getWindow(i)->updateNotebookInfo(p_notebook);
- }
- }
- VEditTab *VEditArea::getCurrentTab() const
- {
- if (curWindowIndex == -1) {
- return NULL;
- }
- VEditWindow *win = getWindow(curWindowIndex);
- return win->getCurrentTab();
- }
- VEditTab *VEditArea::getTab(int p_winIdx, int p_tabIdx) const
- {
- VEditWindow *win = getWindow(p_winIdx);
- if (!win) {
- return NULL;
- }
- return win->getTab(p_tabIdx);
- }
- VEditTab *VEditArea::getTab(const VFile *p_file) const
- {
- int nrWin = splitter->count();
- for (int winIdx = 0; winIdx < nrWin; ++winIdx) {
- VEditWindow *win = getWindow(winIdx);
- int tabIdx = win->findTabByFile(p_file);
- if (tabIdx != -1) {
- return win->getTab(tabIdx);
- }
- }
- return NULL;
- }
- QVector<VEditTabInfo> VEditArea::getAllTabsInfo() const
- {
- QVector<VEditTabInfo> tabs;
- int nrWin = splitter->count();
- for (int i = 0; i < nrWin; ++i) {
- tabs.append(getWindow(i)->getAllTabsInfo());
- }
- return tabs;
- }
- QVector<VEditTab *> VEditArea::getAllTabs() const
- {
- QVector<VEditTab *> tabs;
- int nrWin = splitter->count();
- for (int i = 0; i < nrWin; ++i) {
- tabs.append(getWindow(i)->getAllTabs());
- }
- return tabs;
- }
- int VEditArea::windowIndex(const VEditWindow *p_window) const
- {
- int nrWin = splitter->count();
- for (int i = 0; i < nrWin; ++i) {
- if (p_window == getWindow(i)) {
- return i;
- }
- }
- return -1;
- }
- void VEditArea::moveTab(QWidget *p_widget, int p_fromIdx, int p_toIdx)
- {
- int nrWin = splitter->count();
- if (!p_widget || p_fromIdx < 0 || p_fromIdx >= nrWin
- || p_toIdx < 0 || p_toIdx >= nrWin) {
- return;
- }
- qDebug() << "move widget" << p_widget << "from" << p_fromIdx << "to" << p_toIdx;
- if (!getWindow(p_toIdx)->addEditTab(p_widget)) {
- delete p_widget;
- }
- }
- // Only propogate the search in the IncrementalSearch case.
- void VEditArea::handleFindTextChanged(const QString &p_text, uint p_options)
- {
- VEditTab *tab = getCurrentTab();
- if (tab) {
- if (p_options & FindOption::IncrementalSearch) {
- tab->findText(p_text, p_options, true);
- }
- }
- }
- void VEditArea::handleFindOptionChanged(uint p_options)
- {
- qDebug() << "find option changed" << p_options;
- g_config->setFindCaseSensitive(p_options & FindOption::CaseSensitive);
- g_config->setFindWholeWordOnly(p_options & FindOption::WholeWordOnly);
- g_config->setFindRegularExpression(p_options & FindOption::RegularExpression);
- g_config->setFindIncrementalSearch(p_options & FindOption::IncrementalSearch);
- }
- void VEditArea::handleFindNext(const QString &p_text, uint p_options,
- bool p_forward)
- {
- qDebug() << "find next" << p_text << p_options << p_forward;
- VEditTab *tab = getCurrentTab();
- if (tab) {
- tab->findText(p_text, p_options, false, p_forward);
- }
- }
- void VEditArea::handleReplace(const QString &p_text, uint p_options,
- const QString &p_replaceText, bool p_findNext)
- {
- qDebug() << "replace" << p_text << p_options << "with" << p_replaceText
- << p_findNext;
- VEditTab *tab = getCurrentTab();
- if (tab) {
- tab->replaceText(p_text, p_options, p_replaceText, p_findNext);
- }
- }
- void VEditArea::handleReplaceAll(const QString &p_text, uint p_options,
- const QString &p_replaceText)
- {
- qDebug() << "replace all" << p_text << p_options << "with" << p_replaceText;
- VEditTab *tab = getCurrentTab();
- if (tab) {
- tab->replaceTextAll(p_text, p_options, p_replaceText);
- }
- }
- // Let VEditArea get focus after VFindReplaceDialog is closed.
- void VEditArea::handleFindDialogClosed()
- {
- if (curWindowIndex == -1) {
- setFocus();
- } else {
- getWindow(curWindowIndex)->focusWindow();
- }
- // Clear all the search highlight.
- int nrWin = splitter->count();
- for (int i = 0; i < nrWin; ++i) {
- getWindow(i)->clearSearchedWordHighlight();
- }
- }
- QString VEditArea::getSelectedText()
- {
- VEditTab *tab = getCurrentTab();
- if (tab) {
- return tab->getSelectedText();
- } else {
- return QString();
- }
- }
- int VEditArea::focusNextWindow(int p_biaIdx)
- {
- if (p_biaIdx == 0) {
- return curWindowIndex;
- }
- int newIdx = curWindowIndex + p_biaIdx;
- if (newIdx < 0) {
- newIdx = 0;
- } else if (newIdx >= splitter->count()) {
- newIdx = splitter->count() - 1;
- }
- if (newIdx >= 0 && newIdx < splitter->count()) {
- setCurrentWindow(newIdx, true);
- } else {
- newIdx = -1;
- }
- return newIdx;
- }
- void VEditArea::moveCurrentTabOneSplit(bool p_right)
- {
- getWindow(curWindowIndex)->moveCurrentTabOneSplit(p_right);
- }
- VEditWindow *VEditArea::getCurrentWindow() const
- {
- if (curWindowIndex < 0) {
- return NULL;
- }
- return getWindow(curWindowIndex);
- }
- void VEditArea::registerNavigation(QChar p_majorKey)
- {
- m_majorKey = p_majorKey;
- V_ASSERT(m_keyMap.empty());
- V_ASSERT(m_naviLabels.empty());
- }
- void VEditArea::showNavigation()
- {
- // Clean up.
- m_keyMap.clear();
- for (auto label : m_naviLabels) {
- delete label;
- }
- m_naviLabels.clear();
- if (!isVisible()) {
- return;
- }
- // Generate labels for VEDitTab.
- int charIdx = 0;
- for (int i = 0; charIdx < 26 && i < splitter->count(); ++i) {
- VEditWindow *win = getWindow(i);
- QVector<TabNavigationInfo> tabInfos = win->getTabsNavigationInfo();
- for (int j = 0; charIdx < 26 && j < tabInfos.size(); ++j, ++charIdx) {
- QChar key('a' + charIdx);
- m_keyMap[key] = tabInfos[j].m_tab;
- QString str = QString(m_majorKey) + key;
- QLabel *label = new QLabel(str, win);
- label->setStyleSheet(g_vnote->getNavigationLabelStyle(str));
- label->show();
- label->move(tabInfos[j].m_topLeft);
- m_naviLabels.append(label);
- }
- }
- }
- void VEditArea::hideNavigation()
- {
- m_keyMap.clear();
- for (auto label : m_naviLabels) {
- delete label;
- }
- m_naviLabels.clear();
- }
- bool VEditArea::handleKeyNavigation(int p_key, bool &p_succeed)
- {
- bool ret = false;
- p_succeed = false;
- QChar keyChar = VUtils::keyToChar(p_key);
- if (m_isSecondKey && !keyChar.isNull()) {
- m_isSecondKey = false;
- p_succeed = true;
- auto it = m_keyMap.find(keyChar);
- if (it != m_keyMap.end()) {
- ret = true;
- static_cast<VEditTab *>(it.value())->focusTab();
- }
- } else if (keyChar == m_majorKey) {
- // Major key pressed.
- // Need second key if m_keyMap is not empty.
- if (m_keyMap.isEmpty()) {
- p_succeed = true;
- } else {
- m_isSecondKey = true;
- }
- ret = true;
- }
- return ret;
- }
- int VEditArea::openFiles(const QVector<VFileSessionInfo> &p_files, bool p_oneByOne)
- {
- VFile *curFile = NULL;
- int nrOpened = 0;
- for (auto const & info : p_files) {
- QString filePath = VUtils::validFilePathToOpen(info.m_file);
- if (filePath.isEmpty()) {
- continue;
- }
- VFile *file = g_vnote->getFile(filePath);
- if (!file) {
- continue;
- }
- VEditTab *tab = openFile(file, info.m_mode, true);
- ++nrOpened;
- if (info.m_active) {
- curFile = file;
- }
- VEditTabInfo tabInfo;
- tabInfo.m_editTab = tab;
- info.toEditTabInfo(&tabInfo);
- tab->tryRestoreFromTabInfo(tabInfo);
- if (p_oneByOne) {
- QCoreApplication::sendPostedEvents();
- }
- }
- if (curFile) {
- openFile(curFile, OpenFileMode::Read, false);
- }
- return nrOpened;
- }
- void VEditArea::registerCaptainTargets()
- {
- using namespace std::placeholders;
- VCaptain *captain = g_mainWin->getCaptain();
- captain->registerCaptainTarget(tr("ActivateTab1"),
- g_config->getCaptainShortcutKeySequence("ActivateTab1"),
- this,
- std::bind(activateTabByCaptain, _1, _2, 1));
- captain->registerCaptainTarget(tr("ActivateTab2"),
- g_config->getCaptainShortcutKeySequence("ActivateTab2"),
- this,
- std::bind(activateTabByCaptain, _1, _2, 2));
- captain->registerCaptainTarget(tr("ActivateTab3"),
- g_config->getCaptainShortcutKeySequence("ActivateTab3"),
- this,
- std::bind(activateTabByCaptain, _1, _2, 3));
- captain->registerCaptainTarget(tr("ActivateTab4"),
- g_config->getCaptainShortcutKeySequence("ActivateTab4"),
- this,
- std::bind(activateTabByCaptain, _1, _2, 4));
- captain->registerCaptainTarget(tr("ActivateTab5"),
- g_config->getCaptainShortcutKeySequence("ActivateTab5"),
- this,
- std::bind(activateTabByCaptain, _1, _2, 5));
- captain->registerCaptainTarget(tr("ActivateTab6"),
- g_config->getCaptainShortcutKeySequence("ActivateTab6"),
- this,
- std::bind(activateTabByCaptain, _1, _2, 6));
- captain->registerCaptainTarget(tr("ActivateTab7"),
- g_config->getCaptainShortcutKeySequence("ActivateTab7"),
- this,
- std::bind(activateTabByCaptain, _1, _2, 7));
- captain->registerCaptainTarget(tr("ActivateTab8"),
- g_config->getCaptainShortcutKeySequence("ActivateTab8"),
- this,
- std::bind(activateTabByCaptain, _1, _2, 8));
- captain->registerCaptainTarget(tr("ActivateTab9"),
- g_config->getCaptainShortcutKeySequence("ActivateTab9"),
- this,
- std::bind(activateTabByCaptain, _1, _2, 9));
- captain->registerCaptainTarget(tr("AlternateTab"),
- g_config->getCaptainShortcutKeySequence("AlternateTab"),
- this,
- alternateTabByCaptain);
- captain->registerCaptainTarget(tr("OpenedFileList"),
- g_config->getCaptainShortcutKeySequence("OpenedFileList"),
- this,
- showOpenedFileListByCaptain);
- captain->registerCaptainTarget(tr("ActivateSplitLeft"),
- g_config->getCaptainShortcutKeySequence("ActivateSplitLeft"),
- this,
- activateSplitLeftByCaptain);
- captain->registerCaptainTarget(tr("ActivateSplitRight"),
- g_config->getCaptainShortcutKeySequence("ActivateSplitRight"),
- this,
- activateSplitRightByCaptain);
- captain->registerCaptainTarget(tr("MoveTabSplitLeft"),
- g_config->getCaptainShortcutKeySequence("MoveTabSplitLeft"),
- this,
- moveTabSplitLeftByCaptain);
- captain->registerCaptainTarget(tr("MoveTabSplitRight"),
- g_config->getCaptainShortcutKeySequence("MoveTabSplitRight"),
- this,
- moveTabSplitRightByCaptain);
- captain->registerCaptainTarget(tr("ActivateNextTab"),
- g_config->getCaptainShortcutKeySequence("ActivateNextTab"),
- this,
- activateNextTabByCaptain);
- captain->registerCaptainTarget(tr("ActivatePreviousTab"),
- g_config->getCaptainShortcutKeySequence("ActivatePreviousTab"),
- this,
- activatePreviousTabByCaptain);
- captain->registerCaptainTarget(tr("VerticalSplit"),
- g_config->getCaptainShortcutKeySequence("VerticalSplit"),
- this,
- verticalSplitByCaptain);
- captain->registerCaptainTarget(tr("RemoveSplit"),
- g_config->getCaptainShortcutKeySequence("RemoveSplit"),
- this,
- removeSplitByCaptain);
- captain->registerCaptainTarget(tr("MaximizeSplit"),
- g_config->getCaptainShortcutKeySequence("MaximizeSplit"),
- this,
- maximizeSplitByCaptain);
- captain->registerCaptainTarget(tr("DistributeSplits"),
- g_config->getCaptainShortcutKeySequence("DistributeSplits"),
- this,
- distributeSplitsByCaptain);
- captain->registerCaptainTarget(tr("MagicWord"),
- g_config->getCaptainShortcutKeySequence("MagicWord"),
- this,
- evaluateMagicWordsByCaptain);
- captain->registerCaptainTarget(tr("ApplySnippet"),
- g_config->getCaptainShortcutKeySequence("ApplySnippet"),
- this,
- applySnippetByCaptain);
- captain->registerCaptainTarget(tr("LivePreview"),
- g_config->getCaptainShortcutKeySequence("LivePreview"),
- this,
- toggleLivePreviewByCaptain);
- captain->registerCaptainTarget(tr("ExpandLivePreview"),
- g_config->getCaptainShortcutKeySequence("ExpandLivePreview"),
- this,
- expandLivePreviewByCaptain);
- captain->registerCaptainTarget(tr("ParseAndPaste"),
- g_config->getCaptainShortcutKeySequence("ParseAndPaste"),
- this,
- parseAndPasteByCaptain);
- }
- bool VEditArea::activateTabByCaptain(void *p_target, void *p_data, int p_idx)
- {
- Q_UNUSED(p_data);
- VEditArea *obj = static_cast<VEditArea *>(p_target);
- VEditWindow *win = obj->getCurrentWindow();
- if (win) {
- if (win->activateTab(p_idx)) {
- return false;
- }
- }
- return true;
- }
- bool VEditArea::alternateTabByCaptain(void *p_target, void *p_data)
- {
- Q_UNUSED(p_data);
- VEditArea *obj = static_cast<VEditArea *>(p_target);
- VEditWindow *win = obj->getCurrentWindow();
- if (win) {
- if (win->alternateTab()) {
- return false;
- }
- }
- return true;
- }
- bool VEditArea::showOpenedFileListByCaptain(void *p_target, void *p_data)
- {
- Q_UNUSED(p_data);
- VEditArea *obj = static_cast<VEditArea *>(p_target);
- VEditWindow *win = obj->getCurrentWindow();
- if (win) {
- if (win->showOpenedFileList()) {
- return false;
- }
- }
- return true;
- }
- bool VEditArea::activateSplitLeftByCaptain(void *p_target, void *p_data)
- {
- Q_UNUSED(p_data);
- VEditArea *obj = static_cast<VEditArea *>(p_target);
- if (obj->focusNextWindow(-1) > -1) {
- return false;
- }
- return true;
- }
- bool VEditArea::activateSplitRightByCaptain(void *p_target, void *p_data)
- {
- Q_UNUSED(p_data);
- VEditArea *obj = static_cast<VEditArea *>(p_target);
- if (obj->focusNextWindow(1) > -1) {
- return false;
- }
- return true;
- }
- bool VEditArea::moveTabSplitLeftByCaptain(void *p_target, void *p_data)
- {
- Q_UNUSED(p_data);
- VEditArea *obj = static_cast<VEditArea *>(p_target);
- obj->moveCurrentTabOneSplit(false);
- return true;
- }
- bool VEditArea::moveTabSplitRightByCaptain(void *p_target, void *p_data)
- {
- Q_UNUSED(p_data);
- VEditArea *obj = static_cast<VEditArea *>(p_target);
- obj->moveCurrentTabOneSplit(true);
- return true;
- }
- bool VEditArea::activateNextTabByCaptain(void *p_target, void *p_data)
- {
- Q_UNUSED(p_data);
- VEditArea *obj = static_cast<VEditArea *>(p_target);
- VEditWindow *win = obj->getCurrentWindow();
- if (win) {
- win->focusNextTab(true);
- return false;
- }
- return true;
- }
- bool VEditArea::activatePreviousTabByCaptain(void *p_target, void *p_data)
- {
- Q_UNUSED(p_data);
- VEditArea *obj = static_cast<VEditArea *>(p_target);
- VEditWindow *win = obj->getCurrentWindow();
- if (win) {
- win->focusNextTab(false);
- return false;
- }
- return true;
- }
- bool VEditArea::verticalSplitByCaptain(void *p_target, void *p_data)
- {
- Q_UNUSED(p_data);
- VEditArea *obj = static_cast<VEditArea *>(p_target);
- obj->splitCurrentWindow();
- return false;
- }
- bool VEditArea::removeSplitByCaptain(void *p_target, void *p_data)
- {
- Q_UNUSED(p_data);
- VEditArea *obj = static_cast<VEditArea *>(p_target);
- obj->removeCurrentWindow();
- QWidget *nextFocus = obj->getCurrentTab();
- if (nextFocus) {
- nextFocus->setFocus();
- } else {
- g_mainWin->getFileList()->setFocus();
- }
- return false;
- }
- bool VEditArea::maximizeSplitByCaptain(void *p_target, void *p_data)
- {
- Q_UNUSED(p_data);
- VEditArea *obj = static_cast<VEditArea *>(p_target);
- obj->maximizeCurrentSplit();
- return true;
- }
- bool VEditArea::distributeSplitsByCaptain(void *p_target, void *p_data)
- {
- Q_UNUSED(p_data);
- VEditArea *obj = static_cast<VEditArea *>(p_target);
- obj->distributeSplits();
- return true;
- }
- bool VEditArea::evaluateMagicWordsByCaptain(void *p_target, void *p_data)
- {
- VEditArea *obj = static_cast<VEditArea *>(p_target);
- CaptainData *data = static_cast<CaptainData *>(p_data);
- VEditTab *tab = obj->getCurrentTab();
- if (tab
- && (data->m_focusWidgetBeforeCaptain == tab
- || tab->isAncestorOf(data->m_focusWidgetBeforeCaptain))) {
- tab->evaluateMagicWords();
- }
- return true;
- }
- bool VEditArea::applySnippetByCaptain(void *p_target, void *p_data)
- {
- VEditArea *obj = static_cast<VEditArea *>(p_target);
- CaptainData *data = static_cast<CaptainData *>(p_data);
- VEditTab *tab = obj->getCurrentTab();
- if (tab
- && (data->m_focusWidgetBeforeCaptain == tab
- || tab->isAncestorOf(data->m_focusWidgetBeforeCaptain))) {
- tab->applySnippet();
- }
- return true;
- }
- bool VEditArea::toggleLivePreviewByCaptain(void *p_target, void *p_data)
- {
- Q_UNUSED(p_data);
- VEditArea *obj = static_cast<VEditArea *>(p_target);
- VMdTab *tab = dynamic_cast<VMdTab *>(obj->getCurrentTab());
- if (tab) {
- if (tab->toggleLivePreview()) {
- return false;
- }
- }
- return true;
- }
- bool VEditArea::expandLivePreviewByCaptain(void *p_target, void *p_data)
- {
- Q_UNUSED(p_data);
- VEditArea *obj = static_cast<VEditArea *>(p_target);
- VMdTab *tab = dynamic_cast<VMdTab *>(obj->getCurrentTab());
- if (tab) {
- if (tab->expandRestorePreviewArea()) {
- return false;
- }
- }
- return true;
- }
- bool VEditArea::parseAndPasteByCaptain(void *p_target, void *p_data)
- {
- Q_UNUSED(p_data);
- VEditArea *obj = static_cast<VEditArea *>(p_target);
- const VMdTab *tab = dynamic_cast<const VMdTab *>(obj->getCurrentTab());
- if (tab && tab->isEditMode()) {
- VMdEditor *editor = tab->getEditor();
- editor->parseAndPaste();
- }
- return true;
- }
- void VEditArea::recordClosedFile(const VFileSessionInfo &p_file)
- {
- for (auto it = m_lastClosedFiles.begin(); it != m_lastClosedFiles.end(); ++it) {
- if (it->m_file == p_file.m_file) {
- // Remove it.
- m_lastClosedFiles.erase(it);
- break;
- }
- }
- m_lastClosedFiles.push(p_file);
- emit fileClosed(p_file.m_file);
- }
- void VEditArea::handleFileTimerTimeout()
- {
- int nrWin = splitter->count();
- for (int i = 0; i < nrWin; ++i) {
- // Check whether opened files have been changed outside.
- VEditWindow *win = getWindow(i);
- win->checkFileChangeOutside();
- if (m_autoSave) {
- win->saveAll();
- }
- }
- }
- QRect VEditArea::editAreaRect() const
- {
- QRect rt = rect();
- int nrWin = splitter->count();
- if (nrWin > 0) {
- rt.setTopLeft(QPoint(0, getWindow(0)->tabBarHeight()));
- }
- return rt;
- }
- void VEditArea::maximizeCurrentSplit()
- {
- if (splitter->count() <= 1 || curWindowIndex == -1) {
- return;
- }
- const int MinSplitWidth = 20 * VUtils::calculateScaleFactor();
- auto sizes = splitter->sizes();
- int totalWidth = 0;
- for (auto sz : sizes) {
- totalWidth += sz;
- }
- int newWidth = totalWidth - MinSplitWidth * (sizes.size() - 1);
- if (newWidth <= 0) {
- return;
- }
- for (int i = 0; i < sizes.size(); ++i) {
- sizes[i] = (i == curWindowIndex) ? newWidth : MinSplitWidth;
- }
- splitter->setSizes(sizes);
- }
- void VEditArea::distributeSplits()
- {
- if (splitter->count() <= 1) {
- return;
- }
- auto sizes = splitter->sizes();
- int totalWidth = 0;
- for (auto sz : sizes) {
- totalWidth += sz;
- }
- int newWidth = totalWidth / sizes.size();
- if (newWidth <= 0) {
- return;
- }
- for (int i = 0; i < sizes.size(); ++i) {
- sizes[i] = newWidth;
- }
- splitter->setSizes(sizes);
- }
- void VEditArea::nextMatch(bool p_forward)
- {
- VEditTab *tab = getCurrentTab();
- if (!tab) {
- return;
- }
- Q_ASSERT(m_findReplace);
- tab->nextMatch(m_findReplace->textToFind(),
- m_findReplace->options(),
- p_forward);
- }
|