auto adjust window size

This commit is contained in:
Gary Wang 2019-10-01 11:44:35 +08:00
parent 8726a04d95
commit 7a7dcad9c6
5 changed files with 142 additions and 8 deletions

102
icons/app-icon.svg Normal file
View File

@ -0,0 +1,102 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="32"
height="32"
viewBox="0 0 8.4666665 8.4666669"
version="1.1"
id="svg8"
inkscape:version="0.92.4 (5da689c313, 2019-01-14)"
sodipodi:docname="view-fullscreen - 副本.svg">
<defs
id="defs2" />
<sodipodi:namedview
id="base"
pagecolor="#000000"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.69803922"
inkscape:pageshadow="2"
inkscape:zoom="22.4"
inkscape:cx="11.283707"
inkscape:cy="14.931415"
inkscape:document-units="px"
inkscape:current-layer="layer1"
showgrid="false"
units="px"
inkscape:pagecheckerboard="true"
inkscape:window-width="1920"
inkscape:window-height="1001"
inkscape:window-x="-9"
inkscape:window-y="-9"
inkscape:window-maximized="1" />
<metadata
id="metadata5">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title />
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(0,-288.5333)">
<path
style="fill:none;stroke:#ffffff;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="M 1.6393869,293.16031 1.615848,290.14918 H 3.6380209"
id="path837"
inkscape:connector-curvature="0"
sodipodi:nodetypes="ccc" />
<path
style="fill:none;stroke:#ffffff;stroke-width:0.26531255px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="m 6.9097992,291.96816 0.014252,-1.85441 -3.2860303,0.0354"
id="path837-6"
inkscape:connector-curvature="0"
sodipodi:nodetypes="ccc" />
<path
style="fill:none;stroke:#ffffff;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="m 6.9097992,291.96816 0.035006,3.10579 H 4.9226322"
id="path837-9"
inkscape:connector-curvature="0"
sodipodi:nodetypes="ccc" />
<path
style="fill:none;stroke:#ffffff;stroke-width:0.26531255px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="m 1.6393869,293.16031 -0.00244,1.90166 3.2856855,0.012"
id="path837-6-4"
inkscape:connector-curvature="0"
sodipodi:nodetypes="ccc" />
<path
style="fill:none;stroke:#ffffff;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="m 1.6418341,292.9131 0.8740699,-0.51972 c 0,0 0.4960938,-0.29529 0.8622581,-0.11812 0.3661646,0.17718 0.094494,0.0236 0.3661646,0.17718 0.2716702,0.15355 0.2716702,0.16537 0.2716702,0.16537 l 0.5669645,0.60239 c 0,0 0.9921875,1.16937 1.1339284,1.37017 l 0.1299292,0.21261 0.1417411,0.30711"
id="path819"
inkscape:connector-curvature="0"
sodipodi:nodetypes="ccccccscc" />
<path
style="fill:none;stroke:#ffffff;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="m 5.1144902,293.84623 0.4488469,-0.62603 c 0,0 0.3071055,-0.22442 0.5669642,-0.2008 l 0.2598587,0.0236 0.3425409,0.12993 v 0 l 0.2480469,0.14174"
id="path821"
inkscape:connector-curvature="0"
sodipodi:nodetypes="ccccccc" />
<ellipse
style="fill:#ffffff;fill-opacity:0.87058824;stroke:none;stroke-width:0;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="path823"
cx="5.0849609"
cy="291.8028"
rx="0.86816406"
ry="0.8976934" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 4.0 KiB

View File

@ -27,6 +27,7 @@ int main(int argc, char *argv[])
if (!urlList.isEmpty()) {
w.showUrls(urlList);
w.adjustWindowSizeBySceneRect();
}
return a.exec();

View File

@ -22,6 +22,7 @@ MainWindow::MainWindow(QWidget *parent) :
this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
this->setAttribute(Qt::WA_TranslucentBackground, true);
this->setMinimumSize(710, 530);
this->setWindowIcon(QIcon(":/icons/app-icon.svg"));
m_fadeOutAnimation = new QPropertyAnimation(this, "windowOpacity");
m_fadeOutAnimation->setDuration(300);
@ -66,14 +67,7 @@ MainWindow::MainWindow(QWidget *parent) :
connect(m_bottomButtonGroup, &BottomButtonGroup::toggleCheckerboardBtnClicked,
this, [ = ](){ m_graphicsView->toggleCheckerboard(); });
this->setGeometry(
QStyle::alignedRect(
Qt::LeftToRight,
Qt::AlignCenter,
this->size(),
qApp->screenAt(QCursor::pos())->geometry()
)
);
centerWindow();
}
MainWindow::~MainWindow()
@ -86,6 +80,28 @@ void MainWindow::showUrls(const QList<QUrl> &urls)
m_graphicsView->showFromUrlList(urls);
}
void MainWindow::adjustWindowSizeBySceneRect()
{
if (m_graphicsView->transform().m11() < 1) {
// if it scaled down by the resize policy:
QSize screenSize = qApp->screenAt(QCursor::pos())->availableSize();
QSize sceneSize = m_graphicsView->sceneRect().toRect().size();
QSize sceneSizeWithMargins = sceneSize + QSize(20, 20);
if (screenSize.expandedTo(sceneSize) == screenSize) {
// we can show the picture by increase the window size.
if (screenSize.expandedTo(sceneSizeWithMargins) == screenSize) {
this->resize(sceneSizeWithMargins);
} else {
this->resize(screenSize);
}
centerWindow();
} else {
// toggle maximum
showMaximized();
}
}
}
void MainWindow::showEvent(QShowEvent *event)
{
updateWidgetsPosition();
@ -223,6 +239,18 @@ bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *r
#endif // _WIN32
}
void MainWindow::centerWindow()
{
this->setGeometry(
QStyle::alignedRect(
Qt::LeftToRight,
Qt::AlignCenter,
this->size(),
qApp->screenAt(QCursor::pos())->geometry()
)
);
}
void MainWindow::closeWindow()
{
QRect windowRect(this->geometry());

View File

@ -17,6 +17,7 @@ public:
~MainWindow() override;
void showUrls(const QList<QUrl> &urls);
void adjustWindowSizeBySceneRect();
protected slots:
void showEvent(QShowEvent *event) override;
@ -28,6 +29,7 @@ protected slots:
bool nativeEvent(const QByteArray& eventType, void* message, long* result) override;
void centerWindow();
void closeWindow();
void updateWidgetsPosition();

View File

@ -6,5 +6,6 @@
<file>icons/zoom-original.svg</file>
<file>icons/object-rorate-right.svg</file>
<file>icons/view-background-checkerboard.svg</file>
<file>icons/app-icon.svg</file>
</qresource>
</RCC>