* fix: new/opened editors is not auto focused

This commit is contained in:
royqh1979@gmail.com 2021-04-12 00:00:29 +08:00
parent 0d2a82e741
commit 84475f3b26
8 changed files with 177 additions and 35 deletions

View File

@ -4,10 +4,42 @@
<context> <context>
<name>Editor</name> <name>Editor</name>
<message> <message>
<location filename="editor.cpp" line="26"/> <location filename="editor.cpp" line="45"/>
<source>untitled</source> <source>untitled</source>
<translation></translation> <translation></translation>
</message> </message>
<message>
<location filename="editor.cpp" line="127"/>
<source>Error</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="editor.cpp" line="128"/>
<source>Can&apos;t Open File %1:%2</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="editor.cpp" line="182"/>
<source>Failed to Save file %1: %2</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="editor.cpp" line="186"/>
<source>Failed to Open file %1: %2</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="editor.cpp" line="197"/>
<location filename="editor.cpp" line="206"/>
<location filename="editor.cpp" line="232"/>
<source>Fail</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="editor.cpp" line="221"/>
<source>Save As</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>MainWindow</name> <name>MainWindow</name>
@ -18,68 +50,132 @@
</message> </message>
<message> <message>
<location filename="mainwindow.ui" line="77"/> <location filename="mainwindow.ui" line="77"/>
<location filename="mainwindow.ui" line="153"/> <location filename="mainwindow.ui" line="165"/>
<source>Tab 1</source> <source>Tab 1</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="mainwindow.ui" line="82"/> <location filename="mainwindow.ui" line="82"/>
<location filename="mainwindow.ui" line="158"/> <location filename="mainwindow.ui" line="170"/>
<source>Tab 2</source> <source>Tab 2</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="mainwindow.ui" line="177"/> <location filename="mainwindow.ui" line="189"/>
<source>File</source> <source>File</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="mainwindow.ui" line="191"/> <location filename="mainwindow.ui" line="203"/>
<source>toolBar</source> <source>toolBar</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="mainwindow.ui" line="211"/> <location filename="mainwindow.ui" line="223"/>
<source>New</source> <source>New</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="mainwindow.ui" line="221"/> <location filename="mainwindow.ui" line="226"/>
<source>Ctrl+N</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="236"/>
<source>Open...</source> <source>Open...</source>
<translation>...</translation> <translation>...</translation>
</message> </message>
<message> <message>
<location filename="mainwindow.ui" line="231"/> <location filename="mainwindow.ui" line="239"/>
<source>Ctrl+O</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="249"/>
<source>Save</source> <source>Save</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="mainwindow.ui" line="241"/> <location filename="mainwindow.ui" line="252"/>
<source>Ctrl+S</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="262"/>
<source>Save As...</source> <source>Save As...</source>
<translation>...</translation> <translation>...</translation>
</message> </message>
<message> <message>
<location filename="mainwindow.ui" line="244"/> <location filename="mainwindow.ui" line="265"/>
<source>Save As</source> <source>Save As</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="mainwindow.ui" line="254"/> <location filename="mainwindow.ui" line="275"/>
<source>Save All</source> <source>Save All</source>
<translation></translation> <translation></translation>
</message> </message>
<message>
<location filename="mainwindow.ui" line="278"/>
<source>Ctrl+Shift+S</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.cpp" line="51"/>
<source>Line: %1 Col: %2 Lines: %3 Chars: %4</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.cpp" line="99"/>
<source>Open</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>QObject</name> <name>QObject</name>
<message> <message>
<location filename="editorlist.cpp" line="80"/> <location filename="editorlist.cpp" line="76"/>
<source>Save</source> <source>Save</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="editorlist.cpp" line="80"/> <location filename="editorlist.cpp" line="77"/>
<source>Save changes to %1?</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Save changes to %s?</source> <source>Save changes to %s?</source>
<translation>&quot;%s&quot;</translation> <translation type="vanished">&quot;%s&quot;</translation>
</message>
<message>
<location filename="editor.cpp" line="198"/>
<source>File %s is not writable!</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="systemconsts.cpp" line="10"/>
<source>C files</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="systemconsts.cpp" line="11"/>
<source>C++ files</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="systemconsts.cpp" line="12"/>
<source>Header files</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="systemconsts.cpp" line="13"/>
<source>Text files</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="systemconsts.cpp" line="14"/>
<source>All files</source>
<translation type="unfinished"></translation>
</message> </message>
</context> </context>
</TS> </TS>

View File

@ -107,7 +107,6 @@ Editor::Editor(QWidget *parent, const QString& filename,
connect(this, SIGNAL(linesChanged()), connect(this, SIGNAL(linesChanged()),
this,SLOT(onLinesChanged())); this,SLOT(onLinesChanged()));
this->toggleComment
} }
Editor::~Editor() { Editor::~Editor() {
@ -243,6 +242,11 @@ bool Editor::saveAs(){
return true; return true;
} }
void Editor::activate()
{
this->mParentPageControl->setCurrentWidget(this);
}
const QByteArray& Editor::encodingOption() const noexcept{ const QByteArray& Editor::encodingOption() const noexcept{
return mEncodingOption; return mEncodingOption;
} }

View File

@ -46,6 +46,7 @@ public:
void saveFile(const QString& filename); void saveFile(const QString& filename);
bool save(bool force=false, bool reparse=true); bool save(bool force=false, bool reparse=true);
bool saveAs(); bool saveAs();
void activate();
QTabWidget* pageControl() noexcept; QTabWidget* pageControl() noexcept;

View File

@ -5,14 +5,6 @@
#include <mainwindow.h> #include <mainwindow.h>
#include <iconv.h> #include <iconv.h>
EditorList::UpdateLocker::UpdateLocker(EditorList* editorList): mEditorList(editorList){
mEditorList->beginUpdate();
}
EditorList::UpdateLocker::~UpdateLocker() {
mEditorList->endUpdate();
}
EditorList::EditorList(QTabWidget* leftPageWidget, EditorList::EditorList(QTabWidget* leftPageWidget,
QTabWidget* rightPageWidget, QTabWidget* rightPageWidget,
QSplitter* splitter, QSplitter* splitter,
@ -65,7 +57,10 @@ Editor* EditorList::getEditor(int index, QTabWidget* tabsWidget) const {
} }
bool EditorList::closeEditor(Editor* editor, bool transferFocus, bool force) { bool EditorList::closeEditor(Editor* editor, bool transferFocus, bool force) {
UpdateLocker locker(this); // use RAII to correctly pause/resume update of the panel widget beginUpdate();
auto end = finally([this] {
this->endUpdate();
});
if (editor == NULL) if (editor == NULL)
return false; return false;
if (force) { if (force) {
@ -109,7 +104,10 @@ void EditorList::endUpdate() {
} }
bool EditorList::closeAll(bool force) { bool EditorList::closeAll(bool force) {
UpdateLocker locker(this); beginUpdate();
auto end = finally([this] {
this->endUpdate();
});
while (mLeftPageWidget->count()>0) { while (mLeftPageWidget->count()>0) {
if (!closeEditor(getEditor(0,mLeftPageWidget),false,force)) { if (!closeEditor(getEditor(0,mLeftPageWidget),false,force)) {
return false; return false;

View File

@ -17,14 +17,6 @@ public:
lstBoth lstBoth
}; };
class UpdateLocker {
public:
UpdateLocker(EditorList* editorList);
~UpdateLocker();
private:
EditorList* mEditorList;
};
explicit EditorList(QTabWidget* leftPageWidget, explicit EditorList(QTabWidget* leftPageWidget,
QTabWidget* rightPageWidget, QTabWidget* rightPageWidget,
QSplitter* splitter, QSplitter* splitter,

View File

@ -2,10 +2,17 @@
#include "settings.h" #include "settings.h"
#include "systemconsts.h" #include "systemconsts.h"
#include <QApplication> #include <QApplication>
#include <QDir>
#include <QTranslator>
#include <QDebug>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication app(argc, argv); QApplication app(argc, argv);
QTranslator trans;
qDebug()<<QDir::currentPath();
trans.load(("RedPandaIDE_zh_CN"));
app.installTranslator(&trans);
SystemConsts systemConsts; SystemConsts systemConsts;
pSystemConsts = &systemConsts; pSystemConsts = &systemConsts;
Settings settings; Settings settings;

View File

@ -56,6 +56,9 @@ void MainWindow::updateStatusBarForEditingInfo(int line,int col,int lines,int ch
void MainWindow::openFiles(const QStringList &files) void MainWindow::openFiles(const QStringList &files)
{ {
mEditorList->beginUpdate(); mEditorList->beginUpdate();
auto end = finally([this] {
this->mEditorList->endUpdate();
});
for (QString file:files) { for (QString file:files) {
openFile(file); openFile(file);
} }
@ -66,12 +69,12 @@ void MainWindow::openFile(const QString &filename)
{ {
Editor* editor = mEditorList->findOpenedEditor(filename); Editor* editor = mEditorList->findOpenedEditor(filename);
if (editor!=nullptr) { if (editor!=nullptr) {
editor->setFocus(); editor->activate();
return; return;
} }
editor = mEditorList->newEditor(filename,ENCODING_AUTO_DETECT, editor = mEditorList->newEditor(filename,ENCODING_AUTO_DETECT,
false,false); false,false);
editor->setFocus(); editor->activate();
this->updateStatusBarForEncoding(); this->updateStatusBarForEncoding();
} }
@ -83,6 +86,7 @@ void MainWindow::setupActions() {
void MainWindow::on_actionNew_triggered() void MainWindow::on_actionNew_triggered()
{ {
Editor * editor=mEditorList->newEditor("",ENCODING_AUTO_DETECT,false,true); Editor * editor=mEditorList->newEditor("",ENCODING_AUTO_DETECT,false,true);
editor->activate();
updateStatusBarForEncoding(); updateStatusBarForEncoding();
} }

View File

@ -1,6 +1,10 @@
#ifndef UTILS_H #ifndef UTILS_H
#define UTILS_H #define UTILS_H
#include <type_traits>
#include <utility>
class QByteArray; class QByteArray;
class QString; class QString;
@ -9,8 +13,44 @@ class QString;
#define ENCODING_UTF8_BOM "UTF-8 BOM" #define ENCODING_UTF8_BOM "UTF-8 BOM"
#define ENCODING_SYSTEM_DEFAULT "SYSTEM" #define ENCODING_SYSTEM_DEFAULT "SYSTEM"
#define ENCODING_ASCII "ASCII" #define ENCODING_ASCII "ASCII"
const QByteArray GuessTextEncoding(const QByteArray& text); const QByteArray GuessTextEncoding(const QByteArray& text);
bool isTextAllAscii(const QString& text); bool isTextAllAscii(const QString& text);
template <class F>
class final_action
{
public:
static_assert(!std::is_reference<F>::value && !std::is_const<F>::value &&
!std::is_volatile<F>::value,
"Final_action should store its callable by value");
explicit final_action(F f) noexcept : f_(std::move(f)) {}
final_action(final_action&& other) noexcept
: f_(std::move(other.f_)), invoke_(std::exchange(other.invoke_, false))
{}
final_action(const final_action&) = delete;
final_action& operator=(const final_action&) = delete;
final_action& operator=(final_action&&) = delete;
~final_action() noexcept
{
if (invoke_) f_();
}
private:
F f_;
bool invoke_{true};
};
template <class F> final_action<typename std::remove_cv<typename std::remove_reference<F>::type>::type>
finally(F&& f) noexcept
{
return final_action<typename std::remove_cv<typename std::remove_reference<F>::type>::type>(
std::forward<F>(f));
}
#endif // UTILS_H #endif // UTILS_H