Skip to content

Commit

Permalink
Some changes about tabs (#71)
Browse files Browse the repository at this point in the history
* fix(appwindow.cpp): change competitive companion logic

Use the competitive companion logic introduced in 22af606 and 1b1058a (don't open new tabs).

* style: format codes

* fix & refactor(openfile): use openFile() for opening files

Use AppWindow::openFile() for opening files.

Thus fix bug of opening files via command line arguments / drag & drop.

Duplicate files were not detected when opening via these two ways before.

* feat(appwindow.cpp): show file path in window title

* feat: change to the tab while close confirming

* fix(appwindow.cpp): change window title after closing all tabs

* fix(appwindow): move openFile() to private

* refactor(appwindow.cpp): use openFile() for new tab

* fix: fix bugs when all tabs are closed

Segment fault in the following situations are fixed:

1. all tabs are closed, apply settings in preference window

2. all tabs are closed, received a competitive companion request

After fixing, if all tabs are closed and a competitive companion
request is received, a new tab will be opened for it.

* feat: use "untitled" instead of "Unsaved file"

This is the same as many other text editors, for example, Sublime Text.

"Unsaved file" is a little confusing, it may be understood as "file with
unsaved changes".

* feat & refactor: add Save All and refactor codes

* fix: fix a segmentation fault

To reproduce the segmentation fault:

1. open the editor
2. open a new tab (then there are two tabs, and the current tab is the second one)
3. close the first tab

If you switch to the first tab and close the second one, everything works well.

It's caused by not updated windowIndex.

It's fixed by updating windowsIndex in onTabChanged().

* feat(appwindow.cpp): make tabs movable

* refactor: use pointer in signal and tabWidget->indexOf()
  • Loading branch information
ouuan authored and coder3101 committed Jan 9, 2020
1 parent 83aa681 commit 3fa4eb0
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 166 deletions.
4 changes: 2 additions & 2 deletions include/SettingsManager.hpp
Expand Up @@ -29,7 +29,8 @@
namespace Settings
{

enum ViewMode{
enum ViewMode
{
FULL_EDITOR,
FULL_IO,
SPLIT
Expand Down Expand Up @@ -190,7 +191,6 @@ class SettingManager
QKeySequence getHotkeyCompile();
QKeySequence getHotkeyViewModeToggler();


private:
QString mSettingsFile;
QSettings *mSettings;
Expand Down
16 changes: 11 additions & 5 deletions include/appwindow.hpp
Expand Up @@ -19,7 +19,7 @@ class AppWindow : public QMainWindow

public:
explicit AppWindow(QWidget *parent = nullptr);
explicit AppWindow(QVector<MainWindow *> tabs, QWidget *parent = nullptr);
explicit AppWindow(QStringList args, QWidget *parent = nullptr);
~AppWindow() override;

void closeEvent(QCloseEvent *event) override;
Expand Down Expand Up @@ -47,13 +47,15 @@ class AppWindow : public QMainWindow

void on_actionSave_triggered();

void on_actionSave_as_triggered();
void on_actionSave_As_triggered();

void on_actionSave_All_triggered();

void on_actionCheck_for_updates_triggered();

void onTabCloseRequested(int);
void onTabChanged(int);
void onEditorTextChanged(bool);
void onEditorTextChanged(bool, MainWindow *);
void onSaveTimerElapsed();
void onSettingsApplied();
void onSplitterMoved(int, int);
Expand All @@ -78,11 +80,12 @@ class AppWindow : public QMainWindow

void on_actionSplit_Mode_triggered();

private:
void on_confirmTriggered(MainWindow *widget);

private:
Ui::AppWindow *ui;
MessageLogger *activeLogger = nullptr;
QTimer *timer = nullptr;
QMetaObject::Connection activeTextChangeConnections;
QMetaObject::Connection activeSplitterMoveConnections;
QMetaObject::Connection companionEditorConnections;
Settings::SettingManager *settingManager = nullptr;
Expand All @@ -97,6 +100,9 @@ class AppWindow : public QMainWindow
void saveSettings();
QVector<QShortcut *> hotkeyObjects;
void maybeSetHotkeys();
void closeAll();
bool closeTab(int index);
void openFile(QString fileName);
};

#endif // APPWINDOW_HPP
7 changes: 3 additions & 4 deletions include/mainwindow.hpp
Expand Up @@ -53,9 +53,7 @@ class MainWindow : public QMainWindow
void save(bool force);
void saveAs();

int windowIndeX() const;

bool closeChangedConfirm();
bool closeConfirm();

void killProcesses();
void detachedExecution();
Expand Down Expand Up @@ -98,7 +96,8 @@ class MainWindow : public QMainWindow
void on_changeLanguageButoon_clicked();

signals:
void editorTextChanged(bool isUnsaved);
void editorTextChanged(bool isUnsaved, MainWindow *widget);
void confirmTriggered(MainWindow *widget);

private:
const int windowIndex;
Expand Down
12 changes: 8 additions & 4 deletions src/CompanionServer.cpp
Expand Up @@ -44,7 +44,8 @@ void CompanionServer::updatePort(int port)
// server->setMaxPendingConnections(1);
QObject::connect(server, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
server->listen(QHostAddress::LocalHost, static_cast<unsigned short>(port));
log->warn("Companion", "Port changed to " + std::to_string(port));
if (log != nullptr)
log->warn("Companion", "Port changed to " + std::to_string(port));
}

CompanionServer::~CompanionServer()
Expand All @@ -66,7 +67,8 @@ void CompanionServer::onReadReady()

if (request.startsWith("POST") && request.contains("Content-Type: application/json"))
{
log->info("Companion", "Got a POST Request");
if (log != nullptr)
log->info("Companion", "Got a POST Request");

socket->write("HTTP/1.1 OK\r\n"); // \r needs to be before \n
socket->write("Content-Type: text/html\r\n");
Expand Down Expand Up @@ -107,12 +109,14 @@ void CompanionServer::onReadReady()
}
else
{
log->error("Companion", "JSONParser reported errors. \n" + error.errorString().toStdString(), true);
if (log != nullptr)
log->error("Companion", "JSONParser reported errors. \n" + error.errorString().toStdString(), true);
}
}
else
{
log->warn("Companion", "An Invalid Payload was delivered on the listening port");
if (log != nullptr)
log->warn("Companion", "An Invalid Payload was delivered on the listening port");
socket->write("HTTP/1.1 OK\r\n"); // \r needs to be before \n
socket->write("Content-Type: text/html\r\n");
socket->write("Connection: close\r\n");
Expand Down
18 changes: 12 additions & 6 deletions src/SettingsManager.cpp
Expand Up @@ -374,17 +374,23 @@ void SettingManager::setHotkeyFormat(QKeySequence sequence)
ViewMode SettingManager::getViewMode()
{
QString strings = mSettings->value("view_mode", "split").toString();
if(strings == "split") return Settings::ViewMode::SPLIT;
else if(strings == "code") return Settings::ViewMode::FULL_EDITOR;
else return Settings::ViewMode::FULL_IO;
if (strings == "split")
return Settings::ViewMode::SPLIT;
else if (strings == "code")
return Settings::ViewMode::FULL_EDITOR;
else
return Settings::ViewMode::FULL_IO;
}

void SettingManager::setViewMode(ViewMode v)
{
QString ans;
if(v == Settings::FULL_EDITOR) ans = "code";
else if(v == Settings::FULL_IO) ans = "io";
else ans = "split";
if (v == Settings::FULL_EDITOR)
ans = "code";
else if (v == Settings::FULL_IO)
ans = "io";
else
ans = "split";
mSettings->setValue("view_mode", ans);
}

Expand Down

0 comments on commit 3fa4eb0

Please sign in to comment.