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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
#include "mdiwindow.h"
#include "mdichild.h"
#include "mdiarea.h"
#include "iconmanager.h"
#include "mainwindow.h"
#include "services/dbmanager.h"
#include "db/db.h"
#include "uiconfig.h"
#include <QDateTime>
#include <QApplication>
#include <QDebug>
#include <QFocusEvent>
#include <QAction>
#include <QMessageBox>
#include <QAbstractButton>
MdiWindow::MdiWindow(MdiChild* mdiChild, MdiArea *mdiArea, Qt::WindowFlags flags) :
QMdiSubWindow(mdiArea->viewport(), flags), mdiArea(mdiArea)
{
setAttribute(Qt::WA_DeleteOnClose);
setWidget(mdiChild);
connect(DBLIST, SIGNAL(dbAboutToBeDisconnected(Db*,bool&)), this, SLOT(dbAboutToBeDisconnected(Db*,bool&)));
connect(DBLIST, SIGNAL(dbDisconnected(Db*)), this, SLOT(dbDisconnected(Db*)));
}
MdiWindow::~MdiWindow()
{
if (SQLITESTUDIO->getImmediateQuit())
return;
if (!closeWithoutSessionSaving && !MAINWINDOW->isClosingApp())
MAINWINDOW->pushClosedWindowSessionValue(saveSession());
mdiArea->windowDestroyed(this);
}
QVariant MdiWindow::saveSession()
{
if (!widget())
return QVariant();
QHash<QString, QVariant> hash = getMdiChild()->getSessionValue().toHash();
hash["title"] = windowTitle();
hash["position"] = pos();
hash["geometry"] = saveGeometry();
return hash;
}
bool MdiWindow::restoreSession(const QVariant& sessionValue)
{
if (!widget())
return true;
QHash<QString, QVariant> value = sessionValue.toHash();
if (value.size() == 0)
return true;
if (value.contains("geometry"))
restoreGeometry(value["geometry"].toByteArray());
if (value.contains("position"))
move(value["position"].toPoint());
if (value.contains("title"))
{
QString title = value["title"].toString();
rename(title);
}
return getMdiChild()->applySessionValue(sessionValue);
}
MdiChild* MdiWindow::getMdiChild() const
{
if (!widget())
return nullptr;
return dynamic_cast<MdiChild*>(widget());
}
void MdiWindow::setWidget(MdiChild* value)
{
QMdiSubWindow::setWidget(value);
if (value)
value->setMdiWindow(this);
}
bool MdiWindow::restoreSessionNextTime()
{
return getMdiChild()->restoreSessionNextTime();
}
void MdiWindow::rename(const QString& title)
{
setWindowTitle(title);
QAction* task = mdiArea->getTaskByWindow(this);
if (task)
task->setText(title);
}
void MdiWindow::changeEvent(QEvent* event)
{
if (event->type() != QEvent::WindowStateChange)
{
QMdiSubWindow::changeEvent(event);
return;
}
QWindowStateChangeEvent *changeEvent = static_cast<QWindowStateChangeEvent *>(event);
bool wasActive = changeEvent->oldState().testFlag(Qt::WindowActive);
bool isActive = windowState().testFlag(Qt::WindowActive);
/*
* This is a hack for a bug in Qt: QTBUG-23515. The problem is that when QMdiSubWindow
* changes its state (because we used shortcut, or the TaskBar to activate another window,
* or window is maximized or minimized), then the code responsible for hiding old window
* gives focus to its previous or next child widget (depending on what is the state change),
* before the new window is shown. This seems to be happening in QWidgetPrivate::hide_helper().
*
*/
if (wasActive && isActive)
{
// Handle problem with maximize/minimize
QWidget* w = focusWidget();
QMdiSubWindow::changeEvent(event);
if (w)
w->setFocus();
}
else if (wasActive && !isActive)
{
// Handle problem with switching between 2 MDI windows - part 1
lastFocusedWidget = focusWidget();
QMdiSubWindow::changeEvent(event);
}
else if (!wasActive && isActive)
{
// Handle problem with switching between 2 MDI windows - part 2
QMdiSubWindow::changeEvent(event);
if (!lastFocusedWidget.isNull() && (!focusWidget() || !isAncestorOf(focusWidget())))
lastFocusedWidget->setFocus();
}
else
QMdiSubWindow::changeEvent(event);
if (!MAINWINDOW->isClosingApp())
{
bool wasMaximized = changeEvent->oldState().testFlag(Qt::WindowMaximized);
bool isMaximized = windowState().testFlag(Qt::WindowMaximized);
if (wasMaximized != isMaximized && CFG_UI.General.OpenMaximized.get() != isMaximized)
CFG_UI.General.OpenMaximized.set(isMaximized);
}
}
void MdiWindow::closeEvent(QCloseEvent* e)
{
if (dbBeingClosed || MAINWINDOW->isClosingApp() || !getMdiChild()->isUncommitted())
{
QMdiSubWindow::closeEvent(e);
return;
}
if (confirmClose())
QMdiSubWindow::closeEvent(e);
else
e->ignore();
}
void MdiWindow::dbAboutToBeDisconnected(Db* db, bool& deny)
{
if (!isAssociatedWithDb(db))
return;
if (MAINWINDOW->isClosingApp())
return;
if (getMdiChild()->isUncommitted() && !confirmClose())
deny = true;
else
dbBeingClosed = true;
}
void MdiWindow::dbDisconnected(Db* db)
{
if (!isAssociatedWithDb(db))
return;
if (MAINWINDOW->isClosingApp())
return;
closeWindow();
}
bool MdiWindow::confirmClose()
{
QMessageBox msgBox(QMessageBox::Question, tr("Uncommitted changes"), getMdiChild()->getQuitUncommittedConfirmMessage(), QMessageBox::Yes|QMessageBox::No, this);
msgBox.setDefaultButton(QMessageBox::No);
QAbstractButton* closeBtn = msgBox.button(QMessageBox::Yes);
QAbstractButton* cancelBtn = msgBox.button(QMessageBox::No);
closeBtn->setText(tr("Close anyway"));
closeBtn->setIcon(ICONS.CLOSE);
cancelBtn->setText(tr("Don't close"));
cancelBtn->setIcon(ICONS.GO_BACK);
return (msgBox.exec() == QMessageBox::Yes);
}
void MdiWindow::closeWindow()
{
getMdiChild()->dbClosedFinalCleanup();
MDIAREA->enforceCurrentTaskSelectionAfterWindowClose();
close();
}
bool MdiWindow::isAssociatedWithDb(Db* db)
{
if (dbBeingClosed)
return true; // it was already confirmed by dbAboutToBeDisconnected() and any changes to the "db" member afterwards should not inflict decision change here
return db && getMdiChild()->getAssociatedDb() == db;
}
bool MdiWindow::getCloseWithoutSessionSaving() const
{
return closeWithoutSessionSaving;
}
void MdiWindow::setCloseWithoutSessionSaving(bool value)
{
closeWithoutSessionSaving = value;
}
|