Add functions for positioning

This commit is contained in:
Martchus 2016-08-27 14:54:19 +02:00
parent 9e61cf8e18
commit 3ea0e47a53
2 changed files with 44 additions and 0 deletions

View File

@ -7,6 +7,11 @@
# include <QPalette>
# include <QWidget>
# include <QStyle>
# ifdef GUI_QTWIDGETS
# include <QApplication>
# include <QDesktopWidget>
# include <QCursor>
# endif
#endif
#include <QFileInfo>
#include <QDir>
@ -84,6 +89,43 @@ const QString &dialogStyle()
# ifdef GUI_QTWIDGETS
/*!
* \brief Moves the specified \a widget in the middle of the (available) screen area. If there are multiple
* screens available, the screen where the cursor currently is located is chosen.
*/
void centerWidget(QWidget *widget)
{
widget->setGeometry(
QStyle::alignedRect(
Qt::LeftToRight,
Qt::AlignCenter,
widget->size(),
QApplication::desktop()->availableGeometry(QCursor::pos())
)
);
}
/*!
* \brief Moves the specified \a widget to the corner which is closest to the current cursor position.
* If there are multiple screens available, the screen where the cursor currently is located is chosen.
*/
void cornerWidget(QWidget *widget)
{
const QPoint cursorPos(QCursor::pos());
const QRect availableGeometry(QApplication::desktop()->availableGeometry(cursorPos));
Qt::Alignment alignment = 0;
alignment |= (cursorPos.x() - availableGeometry.left() < availableGeometry.right() - cursorPos.x() ? Qt::AlignLeft : Qt::AlignRight);
alignment |= (cursorPos.y() - availableGeometry.top() < availableGeometry.bottom() - cursorPos.y() ? Qt::AlignTop : Qt::AlignBottom);
widget->setGeometry(
QStyle::alignedRect(
Qt::LeftToRight,
alignment,
widget->size(),
availableGeometry
)
);
}
/*!
* \brief Makes \a widget a heading.
*/

View File

@ -29,6 +29,8 @@ QColor LIB_EXPORT instructionTextColor();
# endif
const QString LIB_EXPORT &dialogStyle();
# ifdef GUI_QTWIDGETS
void LIB_EXPORT centerWidget(QWidget *widget);
void LIB_EXPORT cornerWidget(QWidget *widget);
void LIB_EXPORT makeHeading(QWidget *widget);
void LIB_EXPORT updateStyle(QWidget *widget);
# endif