RedPanda-CPP/RedPandaIDE/editorlist.cpp

143 lines
4.0 KiB
C++
Raw Normal View History

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>
#include <iconv.h>
2021-04-06 23:10:57 +08:00
2021-04-09 10:08:05 +08:00
EditorList::UpdateLocker::UpdateLocker(EditorList* editorList): mEditorList(editorList){
mEditorList->beginUpdate();
}
EditorList::UpdateLocker::~UpdateLocker() {
mEditorList->endUpdate();
}
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
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)
parentPageControl = getNewEditorPageControl();
2021-04-07 21:13:15 +08:00
else
parentPageControl = page;
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;
}
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
}
bool EditorList::closeEditor(Editor* editor, bool transferFocus, bool force) {
2021-04-09 10:08:05 +08:00
UpdateLocker locker(this); // use RAII to correctly pause/resume update of the panel widget
if (editor == NULL)
return false;
if (force) {
editor->save(true,false);
2021-04-09 17:48:25 +08:00
} else if ( (editor->isModified()) && (!editor->text().isEmpty())) {
2021-04-09 10:08:05 +08:00
// ask user if he wants to save
QMessageBox::StandardButton reply;
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-04-07 22:44:08 +08:00
delete editor;
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();
}
}
bool EditorList::closeAll(bool force) {
UpdateLocker locker(this);
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
Editor* EditorList::findOpenedEditor(const QString &filename)
{
for (int i=0;i<mLeftPageWidget->count();i++) {
Editor* e = static_cast<Editor*>(mLeftPageWidget->widget(i));
if (e->filename() == filename) {
return e;
}
}
for (int i=0;i<mRightPageWidget->count();i++) {
Editor* e = static_cast<Editor*>(mRightPageWidget->widget(i));
if (e->filename() == filename) {
return e;
}
}
return nullptr;
}