Add hideOnBlur and TOP position to FramelessDialog

This commit is contained in:
Ryan Huffman 2014-04-28 08:59:28 -07:00
parent 23397a4b10
commit 9bf3b3c6bf
2 changed files with 38 additions and 19 deletions

View file

@ -19,7 +19,8 @@ FramelessDialog::FramelessDialog(QWidget *parent, Qt::WindowFlags flags, Positio
_isResizing(false),
_resizeInitialWidth(0),
_selfHidden(false),
_position(position) {
_position(position),
_hideOnBlur(true) {
setAttribute(Qt::WA_DeleteOnClose);
@ -43,7 +44,7 @@ bool FramelessDialog::eventFilter(QObject* sender, QEvent* event) {
}
break;
case QEvent::WindowStateChange:
if (parentWidget()->isMinimized()) {
if (_hideOnBlur && parentWidget()->isMinimized()) {
if (isVisible()) {
_selfHidden = true;
setHidden(true);
@ -55,7 +56,7 @@ bool FramelessDialog::eventFilter(QObject* sender, QEvent* event) {
break;
case QEvent::ApplicationDeactivate:
// hide on minimize and focus lost
if (isVisible()) {
if (_hideOnBlur && isVisible()) {
_selfHidden = true;
setHidden(true);
}
@ -84,18 +85,23 @@ void FramelessDialog::setStyleSheetFile(const QString& fileName) {
void FramelessDialog::showEvent(QShowEvent* event) {
resizeAndPosition();
QDialog::showEvent(event);
}
void FramelessDialog::resizeAndPosition(bool resizeParent) {
// keep full app height
setFixedHeight(parentWidget()->size().height());
// keep full app height or width depending on position
if (_position == POSITION_LEFT || _position == POSITION_RIGHT) {
setFixedHeight(parentWidget()->size().height());
} else {
setFixedWidth(parentWidget()->size().width());
}
// resize parrent if width is smaller than this dialog
if (resizeParent && parentWidget()->size().width() < size().width()) {
parentWidget()->resize(size().width(), parentWidget()->size().height());
}
if (_position == POSITION_LEFT) {
if (_position == POSITION_LEFT || _position == POSITION_TOP) {
// move to upper left corner
move(parentWidget()->geometry().topLeft());
} else if (_position == POSITION_RIGHT) {
@ -104,16 +110,26 @@ void FramelessDialog::resizeAndPosition(bool resizeParent) {
pos.setX(pos.x() - size().width());
move(pos);
}
repaint();
}
void FramelessDialog::mousePressEvent(QMouseEvent* mouseEvent) {
if (mouseEvent->button() == Qt::LeftButton) {
bool hitLeft = _position == POSITION_LEFT && abs(mouseEvent->pos().x() - size().width()) < RESIZE_HANDLE_WIDTH;
bool hitRight = _position == POSITION_RIGHT && mouseEvent->pos().x() < RESIZE_HANDLE_WIDTH;
if (hitLeft || hitRight) {
_isResizing = true;
_resizeInitialWidth = size().width();
QApplication::setOverrideCursor(Qt::SizeHorCursor);
if (_position == POSITION_LEFT || _position == POSITION_RIGHT) {
bool hitLeft = _position == POSITION_LEFT && abs(mouseEvent->pos().x() - size().width()) < RESIZE_HANDLE_WIDTH;
bool hitRight = _position == POSITION_RIGHT && mouseEvent->pos().x() < RESIZE_HANDLE_WIDTH;
if (hitLeft || hitRight) {
_isResizing = true;
_resizeInitialWidth = size().width();
QApplication::setOverrideCursor(Qt::SizeHorCursor);
}
} else {
bool hitTop = _position == POSITION_TOP && abs(mouseEvent->pos().y() - size().height()) < RESIZE_HANDLE_WIDTH;
if (hitTop) {
_isResizing = true;
_resizeInitialWidth = size().height();
QApplication::setOverrideCursor(Qt::SizeHorCursor);
}
}
}
}
@ -133,6 +149,8 @@ void FramelessDialog::mouseMoveEvent(QMouseEvent* mouseEvent) {
resizeAndPosition();
_resizeInitialWidth = size().width();
setUpdatesEnabled(true);
} else if (_position == POSITION_TOP) {
resize(size().width(), mouseEvent->pos().y());
}
}
}

View file

@ -17,13 +17,15 @@
class FramelessDialog : public QDialog {
Q_OBJECT
public:
enum Position { POSITION_LEFT, POSITION_RIGHT };
FramelessDialog(QWidget* parent = 0, Qt::WindowFlags flags = 0,
Position position = POSITION_LEFT);
public:
enum Position { POSITION_LEFT, POSITION_RIGHT, POSITION_TOP };
FramelessDialog(QWidget* parent, Qt::WindowFlags flags = 0, Position position = POSITION_LEFT);
void setStyleSheetFile(const QString& fileName);
void setHideOnBlur(bool hideOnBlur) { _hideOnBlur = hideOnBlur; };
bool getHideOnBlur() { return _hideOnBlur; };
void resizeAndPosition(bool resizeParent = true);
protected:
virtual void mouseMoveEvent(QMouseEvent* mouseEvent);
@ -34,12 +36,11 @@ protected:
bool eventFilter(QObject* sender, QEvent* event);
private:
void resizeAndPosition(bool resizeParent = true);
bool _isResizing;
int _resizeInitialWidth;
bool _selfHidden; ///< true when the dialog itself because of a window event (deactivation or minimization)
Position _position;
bool _hideOnBlur;
};