| 1 | #include "WindowData.h" |
| 2 | |
| 3 | #include <QScreen> |
| 4 | #include <QGuiApplication> |
| 5 | #include <QDebug> |
| 6 | |
| 7 | #include "jsonutils.h" |
| 8 | |
| 9 | WindowData::WindowData(QObject *parent) : QObject(parent) |
| 10 | { |
| 11 | |
| 12 | } |
| 13 | |
| 14 | QJsonObject WindowData::saveState() const |
| 15 | { |
| 16 | QJsonObject json; |
| 17 | json["rect"] = rectToJsonArray(m_windowRect); |
| 18 | if (!m_screenName.isEmpty()) { |
| 19 | json["screen"] = m_screenName; |
| 20 | } |
| 21 | if (!m_title.isEmpty()) { |
| 22 | json["title"] = m_title; |
| 23 | } |
| 24 | // support frameless option here? |
| 25 | json["state"] = static_cast<int>(m_state); |
| 26 | return json; |
| 27 | } |
| 28 | |
| 29 | bool WindowData::restoreState(QJsonObject state) |
| 30 | { |
| 31 | m_windowRect = jsonArrayToRect(state.value("rect").toArray()); |
| 32 | emit windowRectChanged(m_windowRect); |
| 33 | |
| 34 | if (state.contains("screen")) { |
| 35 | m_screenName = state.value("screen").toString(); |
| 36 | } else { |
| 37 | m_screenName.clear(); |
| 38 | } |
| 39 | |
| 40 | if (state.contains("title")) { |
| 41 | m_title = state.value("title").toString(); |
| 42 | } |
| 43 | |
| 44 | if (state.contains("state")) { |
| 45 | m_state = static_cast<Qt::WindowState>(state.value("state").toInt()); |
| 46 | } |
| 47 | |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | QRect WindowData::windowRect() const |
| 52 | { |
| 53 | return m_windowRect; |
| 54 | } |
| 55 | |
| 56 | QScreen *WindowData::screen() const |
| 57 | { |
| 58 | if (m_screenName.isEmpty()) |
| 59 | return nullptr; |
| 60 | |
| 61 | QStringList screenNames; |
| 62 | Q_FOREACH(auto s, qApp->screens()) { |
| 63 | if (s->name() == m_screenName) { |
| 64 | return s; |
| 65 | } |
| 66 | screenNames.append(s->name()); |
| 67 | } |
| 68 | |
| 69 | qWarning() << "couldn't find a screen with name:" << m_screenName; |
| 70 | qWarning() << "Available screens:" << screenNames.join(", "); |
| 71 | return nullptr; |
| 72 | } |
| 73 | |
| 74 | void WindowData::setWindowState(Qt::WindowState ws) |
| 75 | { |
| 76 | m_state = ws; |
| 77 | } |
| 78 | |
| 79 | void WindowData::setWindowRect(QRect windowRect) |
| 80 | { |
| 81 | if (m_windowRect == windowRect) |
| 82 | return; |
| 83 | |
| 84 | m_windowRect = windowRect; |
| 85 | emit windowRectChanged(m_windowRect); |
| 86 | } |