aboutsummaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/guiSQLiteStudio/common/dialogsizehandler.cpp
blob: ad283ca4ee034c19a2891a1aca0e7a2f37e68dc6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "dialogsizehandler.h"
#include "services/config.h"
#include <QEvent>
#include <QTimer>
#include <QWidget>
#include <QScreen>
#include <QGuiApplication>

DialogSizeHandler::DialogSizeHandler(QObject *parent) :
    DialogSizeHandler(parent->objectName(), parent)
{
}

DialogSizeHandler::DialogSizeHandler(const QString &key, QObject *parent) :
    QObject(parent), configKey(key)
{
    saveTimer = new QTimer(this);
    saveTimer->setInterval(500);
    saveTimer->setSingleShot(true);
    connect(saveTimer, SIGNAL(timeout()), this, SLOT(doSave()));

    QRect geom = CFG->get(CONFIG_GROUP, configKey).toRect();
    if (geom.isValid() && qApp->primaryScreen()->geometry().contains(geom))
    {
        QWidget* w = qobject_cast<QWidget*>(parent);
        w->setGeometry(geom);
    }
}

DialogSizeHandler::~DialogSizeHandler()
{
}

void DialogSizeHandler::applyFor(QObject *parent)
{
    applyFor(parent->objectName(), parent);
}

void DialogSizeHandler::applyFor(const QString &key, QObject *parent)
{
    DialogSizeHandler* handler = new DialogSizeHandler(key, parent);
    parent->installEventFilter(handler);
}

bool DialogSizeHandler::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::Resize || event->type() == QEvent::Move)
    {
        QWidget* w = qobject_cast<QWidget*>(obj);
        recentGeometry = w->geometry();
        saveTimer->start();
    }
    return false;
}

void DialogSizeHandler::doSave()
{
    CFG->set(CONFIG_GROUP, configKey, recentGeometry);
}