Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
fix: Add splitter restorer
Browse files Browse the repository at this point in the history
Added splitter restorer, which reset splitter state if it's broken
Fix #2587
  • Loading branch information
Diadlo committed Feb 26, 2017
1 parent 4b7fc57 commit a231532
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 26 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ set(${PROJECT_NAME}_SOURCES
src/widget/passwordedit.h
src/widget/qrwidget.cpp
src/widget/qrwidget.h
src/widget/splitterrestorer.cpp
src/widget/splitterrestorer.h
src/widget/style.cpp
src/widget/style.h
src/widget/systemtrayicon.cpp
Expand Down
2 changes: 2 additions & 0 deletions qtox.pro
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ HEADERS += \
src/widget/notificationscrollarea.h \
src/widget/passwordedit.h \
src/widget/qrwidget.h \
src/widget/splitterrestorer.h \
src/widget/style.h \
src/widget/systemtrayicon.h \
src/widget/systemtrayicon_private.h \
Expand Down Expand Up @@ -633,6 +634,7 @@ SOURCES += \
src/widget/notificationscrollarea.cpp \
src/widget/passwordedit.cpp \
src/widget/qrwidget.cpp \
src/widget/splitterrestorer.cpp \
src/widget/style.cpp \
src/widget/systemtrayicon.cpp \
src/widget/tool/activatedialog.cpp \
Expand Down
20 changes: 9 additions & 11 deletions src/widget/contentdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

#include "contentdialog.h"
#include "splitterrestorer.h"

#include <QBoxLayout>
#include <QDragEnterEvent>
Expand Down Expand Up @@ -94,29 +95,26 @@ ContentDialog::ContentDialog(SettingsWidget* settingsWidget, QWidget* parent)
splitter->setCollapsible(1, false);
boxLayout->addWidget(splitter);

connect(splitter, &QSplitter::splitterMoved, this, &ContentDialog::saveSplitterState);

const Settings& s = Settings::getInstance();
connect(&s, &Settings::groupchatPositionChanged, this, &ContentDialog::onGroupchatPositionChanged);
connect(splitter, &QSplitter::splitterMoved, this, &ContentDialog::saveSplitterState);

setMinimumSize(500, 220);
setAttribute(Qt::WA_DeleteOnClose);

QByteArray geometry = Settings::getInstance().getDialogGeometry();
QByteArray geometry = s.getDialogGeometry();

if (!geometry.isNull())
if (!geometry.isNull()) {
restoreGeometry(geometry);
else
} else {
resize(720, 400);
}


QByteArray splitterState = Settings::getInstance().getDialogSplitterState();

if (!splitterState.isNull())
splitter->restoreState(splitterState);
QByteArray splitterState = s.getDialogSplitterState();
SplitterRestorer restorer(splitter);
restorer.restore(splitterState, size());

currentDialog = this;

setAcceptDrops(true);

new QShortcut(Qt::CTRL + Qt::Key_Q, this, SLOT(close()));
Expand Down
70 changes: 70 additions & 0 deletions src/widget/splitterrestorer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Copyright © 2017 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
qTox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
*/

#include "src/widget/splitterrestorer.h"

#include <QSplitter>

/**
* @class SplitterRestorer
* @brief Restore splitter from saved state and reset to default
*/

/**
* @brief The width of the default splitter handles.
* By default, this property contains a value that depends on the user's
* platform and style preferences.
*/
static int defaultWidth = 0;

/**
* @brief Width of left splitter size in percents.
*/
const static int leftWidthPercent = 33;

SplitterRestorer::SplitterRestorer(QSplitter* splitter)
: splitter{splitter}
{
if (defaultWidth == 0) {
defaultWidth = QSplitter().handleWidth();
}
}

/**
* @brief Restore splitter from state. And reset in case of error.
* Set the splitter to a reasonnable width by default and on first start
* @param state State saved by QSplitter::saveState()
* @param windowSize Widnow size (used to calculate splitter size)
*/
void SplitterRestorer::restore(const QByteArray& state, const QSize& windowSize)
{
bool brokenSplitter = !splitter->restoreState(state) ||
splitter->orientation() != Qt::Horizontal ||
splitter->handleWidth() > defaultWidth;

if (splitter->count() == 2 && brokenSplitter) {
splitter->setOrientation(Qt::Horizontal);
splitter->setHandleWidth(defaultWidth);
splitter->resize(windowSize);
QList<int> sizes = splitter->sizes();
sizes[0] = splitter->width() * leftWidthPercent / 100;
sizes[1] = splitter->width() - sizes[0];
splitter->setSizes(sizes);
}
}
37 changes: 37 additions & 0 deletions src/widget/splitterrestorer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright © 2017 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
qTox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef SPLITTERRESTORER_H
#define SPLITTERRESTORER_H

class QSize;
class QSplitter;
class QByteArray;

class SplitterRestorer
{
public:
explicit SplitterRestorer(QSplitter* splitter);
void restore(const QByteArray& state, const QSize& windowSize);

private:
QSplitter* splitter;
};

#endif // SPLITTERRESTORER_H
23 changes: 8 additions & 15 deletions src/widget/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "friendwidget.h"
#include "groupwidget.h"
#include "maskablepixmapwidget.h"
#include "splitterrestorer.h"
#include "src/audio/audio.h"
#include "src/core/core.h"
#include "src/core/coreav.h"
Expand Down Expand Up @@ -346,7 +347,7 @@ void Widget::init()
#endif

contentLayout = nullptr;
onSeparateWindowChanged(Settings::getInstance().getSeparateWindow(), false);
onSeparateWindowChanged(s.getSeparateWindow(), false);

ui->addButton->setCheckable(true);
ui->groupButton->setCheckable(true);
Expand All @@ -359,21 +360,13 @@ void Widget::init()
}

//restore window state
restoreGeometry(Settings::getInstance().getWindowGeometry());
restoreState(Settings::getInstance().getWindowState());
if (!ui->mainSplitter->restoreState(Settings::getInstance().getSplitterState()))
{
// Set the status panel (friendlist) to a reasonnable width by default/on first start
constexpr int spWidthPc = 33;
ui->mainSplitter->resize(size());
QList<int> sizes = ui->mainSplitter->sizes();
sizes[0] = ui->mainSplitter->width()*spWidthPc/100;
sizes[1] = ui->mainSplitter->width() - sizes[0];
ui->mainSplitter->setSizes(sizes);
}
restoreGeometry(s.getWindowGeometry());
restoreState(s.getWindowState());
SplitterRestorer restorer(ui->mainSplitter);
restorer.restore(s.getSplitterState(), size());

#if (AUTOUPDATE_ENABLED)
if (Settings::getInstance().getCheckUpdates())
if (s.getCheckUpdates())
AutoUpdater::checkUpdatesAsyncInteractive();
#endif

Expand All @@ -400,7 +393,7 @@ void Widget::init()
retranslateUi();
Translator::registerHandler(std::bind(&Widget::retranslateUi, this), this);

if (!Settings::getInstance().getShowSystemTray())
if (!s.getShowSystemTray())
{
show();
}
Expand Down

0 comments on commit a231532

Please sign in to comment.