2021-04-06 23:10:57 +08:00
|
|
|
#include "editorlist.h"
|
2021-04-07 21:13:15 +08:00
|
|
|
#include "editor.h"
|
2021-04-09 10:08:05 +08:00
|
|
|
#include <QMessageBox>
|
2021-04-07 22:44:08 +08:00
|
|
|
#include <QVariant>
|
2021-04-09 10:08:05 +08:00
|
|
|
#include <mainwindow.h>
|
2021-04-29 20:54:44 +08:00
|
|
|
#include <QDebug>
|
2021-06-23 22:38:02 +08:00
|
|
|
#include <QFileInfo>
|
2021-08-01 23:24:37 +08:00
|
|
|
#include "settings.h"
|
2021-04-06 23:10:57 +08:00
|
|
|
|
2021-04-07 21:13:15 +08:00
|
|
|
EditorList::EditorList(QTabWidget* leftPageWidget,
|
|
|
|
QTabWidget* rightPageWidget,
|
|
|
|
QSplitter* splitter,
|
|
|
|
QWidget* panel):
|
|
|
|
mLeftPageWidget(leftPageWidget),
|
|
|
|
mRightPageWidget(rightPageWidget),
|
|
|
|
mSplitter(splitter),
|
|
|
|
mPanel(panel),
|
|
|
|
mUpdateCount(0)
|
2021-04-06 23:10:57 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2021-04-07 21:13:15 +08:00
|
|
|
|
2021-04-08 10:29:21 +08:00
|
|
|
Editor* EditorList::newEditor(const QString& filename, const QByteArray& encoding,
|
2021-04-07 21:13:15 +08:00
|
|
|
bool inProject, bool newFile,
|
|
|
|
QTabWidget* page) {
|
|
|
|
QTabWidget * parentPageControl = NULL;
|
|
|
|
if (page == NULL)
|
2021-04-08 10:29:21 +08:00
|
|
|
parentPageControl = getNewEditorPageControl();
|
2021-04-07 21:13:15 +08:00
|
|
|
else
|
|
|
|
parentPageControl = page;
|
2021-08-31 14:40:41 +08:00
|
|
|
if (!filename.isEmpty() && QFile(filename).exists()) {
|
|
|
|
pMainWindow->fileSystemWatcher()->addPath(filename);
|
|
|
|
}
|
2021-04-07 21:13:15 +08:00
|
|
|
return new Editor(parentPageControl,filename,encoding,inProject,newFile,parentPageControl);
|
|
|
|
//UpdateLayout;
|
|
|
|
}
|
|
|
|
|
2021-04-09 10:08:05 +08:00
|
|
|
QTabWidget* EditorList::getNewEditorPageControl() const {
|
2021-04-07 21:13:15 +08:00
|
|
|
//todo: return widget depends on layout
|
|
|
|
return mLeftPageWidget;
|
|
|
|
}
|
|
|
|
|
2021-04-09 10:08:05 +08:00
|
|
|
QTabWidget* EditorList::getFocusedPageControl() const {
|
|
|
|
//todo:
|
|
|
|
return mLeftPageWidget;
|
|
|
|
}
|
|
|
|
|
2021-04-08 10:29:21 +08:00
|
|
|
Editor* EditorList::getEditor(int index, QTabWidget* tabsWidget) const {
|
2021-04-07 22:44:08 +08:00
|
|
|
QTabWidget* selectedWidget;
|
|
|
|
if (tabsWidget == NULL) {
|
2021-04-09 10:08:05 +08:00
|
|
|
selectedWidget = getFocusedPageControl(); // todo: get focused widget
|
2021-04-07 22:44:08 +08:00
|
|
|
} else {
|
|
|
|
selectedWidget = tabsWidget;
|
|
|
|
}
|
|
|
|
if (index == -1) {
|
2021-04-09 10:08:05 +08:00
|
|
|
index = selectedWidget->currentIndex();
|
2021-04-07 22:44:08 +08:00
|
|
|
}
|
2021-04-09 10:08:05 +08:00
|
|
|
if (index<0 || index >= selectedWidget->count()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-04-09 17:48:25 +08:00
|
|
|
return (Editor*)selectedWidget->widget(index);
|
2021-04-07 22:44:08 +08:00
|
|
|
}
|
|
|
|
|
2021-04-08 10:29:21 +08:00
|
|
|
bool EditorList::closeEditor(Editor* editor, bool transferFocus, bool force) {
|
2021-04-12 00:00:29 +08:00
|
|
|
beginUpdate();
|
|
|
|
auto end = finally([this] {
|
|
|
|
this->endUpdate();
|
|
|
|
});
|
2021-04-09 10:08:05 +08:00
|
|
|
if (editor == NULL)
|
|
|
|
return false;
|
|
|
|
if (force) {
|
|
|
|
editor->save(true,false);
|
2021-05-24 00:41:00 +08:00
|
|
|
} else if ( (editor->modified()) && (!editor->empty())) {
|
2021-04-09 10:08:05 +08:00
|
|
|
// ask user if he wants to save
|
|
|
|
QMessageBox::StandardButton reply;
|
2021-04-11 12:39:22 +08:00
|
|
|
reply = QMessageBox::question(pMainWindow,QObject::tr("Save"),
|
|
|
|
QString(QObject::tr("Save changes to %1?")).arg(editor->filename()),
|
2021-04-09 10:08:05 +08:00
|
|
|
QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel);
|
|
|
|
if (reply == QMessageBox::Cancel) {
|
|
|
|
return false;
|
|
|
|
} else if (reply == QMessageBox::Yes) {
|
|
|
|
if (!editor->save(false,false)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 17:48:25 +08:00
|
|
|
if (transferFocus && (editor->pageControl()->currentWidget()==editor)) {
|
|
|
|
//todo: activate & focus the previous editor
|
|
|
|
}
|
2021-04-09 10:08:05 +08:00
|
|
|
|
2021-08-01 23:24:37 +08:00
|
|
|
if (pSettings->history().addToOpenedFiles(editor->filename())) {
|
|
|
|
pMainWindow->rebuildOpenedFileHisotryMenu();
|
|
|
|
}
|
|
|
|
|
2021-08-31 14:40:41 +08:00
|
|
|
pMainWindow->fileSystemWatcher()->removePath(editor->filename());
|
2021-08-03 23:55:57 +08:00
|
|
|
//editor->deleteLater();
|
|
|
|
delete editor;
|
2021-08-23 21:50:53 +08:00
|
|
|
|
|
|
|
editor = getEditor();
|
|
|
|
if (!force)
|
|
|
|
pMainWindow->updateClassBrowserForEditor(editor);
|
2021-04-07 22:44:08 +08:00
|
|
|
return true;
|
|
|
|
}
|
2021-04-09 10:08:05 +08:00
|
|
|
|
|
|
|
void EditorList::beginUpdate() {
|
|
|
|
if (mUpdateCount==0) {
|
|
|
|
mPanel->setUpdatesEnabled(false);
|
|
|
|
}
|
|
|
|
mUpdateCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorList::endUpdate() {
|
|
|
|
mUpdateCount--;
|
|
|
|
if (mUpdateCount==0) {
|
|
|
|
mPanel->setUpdatesEnabled(true);
|
|
|
|
mPanel->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 11:02:03 +08:00
|
|
|
void EditorList::applySettings()
|
|
|
|
{
|
|
|
|
for (int i=0;i<mLeftPageWidget->count();i++) {
|
|
|
|
Editor* e = static_cast<Editor*>(mLeftPageWidget->widget(i));
|
|
|
|
e->applySettings();
|
|
|
|
}
|
|
|
|
for (int i=0;i<mRightPageWidget->count();i++) {
|
|
|
|
Editor* e = static_cast<Editor*>(mRightPageWidget->widget(i));
|
|
|
|
e->applySettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-20 14:30:47 +08:00
|
|
|
void EditorList::applyColorSchemes(const QString& name)
|
|
|
|
{
|
|
|
|
for (int i=0;i<mLeftPageWidget->count();i++) {
|
|
|
|
Editor* e = static_cast<Editor*>(mLeftPageWidget->widget(i));
|
|
|
|
e->applyColorScheme(name);
|
|
|
|
}
|
|
|
|
for (int i=0;i<mRightPageWidget->count();i++) {
|
|
|
|
Editor* e = static_cast<Editor*>(mRightPageWidget->widget(i));
|
|
|
|
e->applyColorScheme(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-23 22:38:02 +08:00
|
|
|
bool EditorList::isFileOpened(const QString &name)
|
|
|
|
{
|
|
|
|
QFileInfo fileInfo(name);
|
|
|
|
QString filename = fileInfo.absoluteFilePath();
|
|
|
|
for (int i=0;i<mLeftPageWidget->count();i++) {
|
|
|
|
Editor* e = static_cast<Editor*>(mLeftPageWidget->widget(i));
|
2021-06-24 20:43:09 +08:00
|
|
|
if (e->filename().compare(filename)==0 || e->filename().compare(name)==0)
|
2021-06-23 22:38:02 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (int i=0;i<mRightPageWidget->count();i++) {
|
|
|
|
Editor* e = static_cast<Editor*>(mRightPageWidget->widget(i));
|
2021-06-24 20:43:09 +08:00
|
|
|
if (e->filename().compare(filename)==0 || e->filename().compare(name)==0)
|
2021-06-23 22:38:02 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-26 00:22:08 +08:00
|
|
|
int EditorList::pageCount()
|
|
|
|
{
|
|
|
|
return mLeftPageWidget->count()+mRightPageWidget->count();
|
|
|
|
}
|
|
|
|
|
|
|
|
Editor *EditorList::operator[](int index)
|
|
|
|
{
|
|
|
|
if (index>=0 && index<mLeftPageWidget->count()) {
|
|
|
|
return static_cast<Editor*>(mLeftPageWidget->widget(index));
|
|
|
|
}
|
|
|
|
index -= mLeftPageWidget->count();
|
|
|
|
if (index>=0 && index<mRightPageWidget->count()) {
|
|
|
|
return static_cast<Editor*>(mRightPageWidget->widget(index));
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-04-09 10:08:05 +08:00
|
|
|
bool EditorList::closeAll(bool force) {
|
2021-04-12 00:00:29 +08:00
|
|
|
beginUpdate();
|
|
|
|
auto end = finally([this] {
|
|
|
|
this->endUpdate();
|
|
|
|
});
|
2021-04-09 17:48:25 +08:00
|
|
|
while (mLeftPageWidget->count()>0) {
|
|
|
|
if (!closeEditor(getEditor(0,mLeftPageWidget),false,force)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (mRightPageWidget->count()>0) {
|
|
|
|
if (!closeEditor(getEditor(0,mRightPageWidget),false,force)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-04-09 10:08:05 +08:00
|
|
|
|
2021-04-09 17:48:25 +08:00
|
|
|
return true;
|
2021-04-09 10:08:05 +08:00
|
|
|
}
|
2021-04-11 21:33:08 +08:00
|
|
|
|
2021-04-29 20:54:44 +08:00
|
|
|
Editor* EditorList::getOpenedEditorByFilename(const QString &filename)
|
2021-04-11 21:33:08 +08:00
|
|
|
{
|
2021-06-24 16:05:19 +08:00
|
|
|
QFileInfo fileInfo(filename);
|
|
|
|
QString fullname = fileInfo.absoluteFilePath();
|
2021-04-11 21:33:08 +08:00
|
|
|
for (int i=0;i<mLeftPageWidget->count();i++) {
|
|
|
|
Editor* e = static_cast<Editor*>(mLeftPageWidget->widget(i));
|
2021-06-24 20:43:09 +08:00
|
|
|
if (e->filename().compare(filename)==0 || e->filename().compare(fullname)==0) {
|
2021-04-11 21:33:08 +08:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i=0;i<mRightPageWidget->count();i++) {
|
|
|
|
Editor* e = static_cast<Editor*>(mRightPageWidget->widget(i));
|
2021-06-24 20:43:09 +08:00
|
|
|
if (e->filename().compare(filename)==0 || e->filename().compare(fullname)==0) {
|
2021-04-11 21:33:08 +08:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-04-29 20:54:44 +08:00
|
|
|
|
|
|
|
Editor *EditorList::getEditorByFilename(const QString &filename)
|
|
|
|
{
|
|
|
|
//check if an editor is already openned
|
2021-06-24 20:43:09 +08:00
|
|
|
Editor* e=getOpenedEditorByFilename(filename);
|
2021-04-29 20:54:44 +08:00
|
|
|
if (e!=nullptr)
|
|
|
|
return e;
|
|
|
|
//Todo: check if is in the project
|
|
|
|
|
|
|
|
//Create a new editor
|
2021-06-24 20:43:09 +08:00
|
|
|
QFileInfo fileInfo(filename);
|
|
|
|
QString fullname = fileInfo.absoluteFilePath();
|
2021-06-24 16:05:19 +08:00
|
|
|
if (fileInfo.exists())
|
|
|
|
return newEditor(fullname,ENCODING_AUTO_DETECT,false,false);
|
|
|
|
return nullptr;
|
2021-04-29 20:54:44 +08:00
|
|
|
}
|
2021-08-23 10:16:06 +08:00
|
|
|
|
|
|
|
bool EditorList::getContentFromOpenedEditor(const QString &filename, QStringList &buffer)
|
|
|
|
{
|
|
|
|
Editor * e= getOpenedEditorByFilename(filename);
|
|
|
|
if (!e)
|
|
|
|
return false;
|
|
|
|
buffer = e->lines()->contents();
|
|
|
|
return true;
|
|
|
|
}
|