1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

CallConfirmWidget: track anchor's position

This commit is contained in:
Tux3 / Mlkj / !Lev.uXFMLA 2015-01-25 18:43:30 +01:00
parent b10edb9baf
commit 279debdab9
No known key found for this signature in database
GPG Key ID: 7E086DD661263264
4 changed files with 39 additions and 11 deletions

View File

@ -10,14 +10,12 @@
#include <QRect> #include <QRect>
#include <QPalette> #include <QPalette>
CallConfirmWidget::CallConfirmWidget(QWidget *anchor) : CallConfirmWidget::CallConfirmWidget(const QWidget *Anchor) :
QWidget(Widget::getInstance()), QWidget(Widget::getInstance()), anchor(Anchor),
rectW{130}, rectH{90}, rectW{130}, rectH{90},
spikeW{30}, spikeH{15}, spikeW{30}, spikeH{15},
roundedFactor{15} roundedFactor{15}
{ {
Widget* w = Widget::getInstance();
setWindowFlags(Qt::SubWindow); setWindowFlags(Qt::SubWindow);
QPalette palette; QPalette palette;
@ -36,12 +34,23 @@ CallConfirmWidget::CallConfirmWidget(QWidget *anchor) :
connect(buttonBox, &QDialogButtonBox::accepted, this, &CallConfirmWidget::accepted); connect(buttonBox, &QDialogButtonBox::accepted, this, &CallConfirmWidget::accepted);
connect(buttonBox, &QDialogButtonBox::rejected, this, &CallConfirmWidget::rejected); connect(buttonBox, &QDialogButtonBox::rejected, this, &CallConfirmWidget::rejected);
connect(Widget::getInstance(), &Widget::resized, this, &CallConfirmWidget::reposition);
layout->setMargin(12); layout->setMargin(12);
layout->addSpacing(spikeH); layout->addSpacing(spikeH);
layout->addWidget(callLabel); layout->addWidget(callLabel);
layout->addWidget(buttonBox); layout->addWidget(buttonBox);
QPoint pos = anchor->mapToGlobal({(anchor->width()-width())/2,anchor->height()})-w->mapToGlobal({0,0}); setFixedSize(rectW,rectH+spikeH);
reposition();
}
void CallConfirmWidget::reposition()
{
Widget* w = Widget::getInstance();
QPoint pos = anchor->mapToGlobal({(anchor->width()-rectW)/2,anchor->height()})-w->mapToGlobal({0,0});
// We don't want the widget to overflow past the right of the screen
int xOverflow=0; int xOverflow=0;
if (pos.x() + rectW > w->width()) if (pos.x() + rectW > w->width())
xOverflow = pos.x() + rectW - w->width(); xOverflow = pos.x() + rectW - w->width();
@ -49,12 +58,12 @@ CallConfirmWidget::CallConfirmWidget(QWidget *anchor) :
mainRect = {0,spikeH,rectW,rectH}; mainRect = {0,spikeH,rectW,rectH};
brush = QBrush(QColor(65,65,65)); brush = QBrush(QColor(65,65,65));
spikePoly = QPolygon({{(rectW-spikeW+xOverflow)/2,spikeH}, spikePoly = QPolygon({{(rectW-spikeW)/2+xOverflow,spikeH},
{(rectW+xOverflow)/2,0}, {rectW/2+xOverflow,0},
{(rectW+spikeW+xOverflow)/2,spikeH}}); {(rectW+spikeW)/2+xOverflow,spikeH}});
setFixedSize(rectW,rectH+spikeH);
move(pos); move(pos);
update();
} }
void CallConfirmWidget::paintEvent(QPaintEvent*) void CallConfirmWidget::paintEvent(QPaintEvent*)
@ -67,3 +76,9 @@ void CallConfirmWidget::paintEvent(QPaintEvent*)
painter.drawRoundRect(mainRect, roundedFactor, roundedFactor); painter.drawRoundRect(mainRect, roundedFactor, roundedFactor);
painter.drawPolygon(spikePoly); painter.drawPolygon(spikePoly);
} }
void CallConfirmWidget::showEvent(QShowEvent*)
{
reposition();
update();
}

View File

@ -7,12 +7,16 @@
#include <QBrush> #include <QBrush>
class QPaintEvent; class QPaintEvent;
class QShowEvent;
/// This is a widget with dialog buttons to accept/reject a call
/// It tracks the position of another widget called the anchor
/// and looks like a bubble at the bottom of that widget.
class CallConfirmWidget : public QWidget class CallConfirmWidget : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit CallConfirmWidget(QWidget *anchor); explicit CallConfirmWidget(const QWidget *Anchor);
signals: signals:
void accepted(); void accepted();
@ -20,15 +24,21 @@ signals:
protected: protected:
virtual void paintEvent(QPaintEvent* event) override; virtual void paintEvent(QPaintEvent* event) override;
virtual void showEvent(QShowEvent * event) override;
protected slots:
void reposition(); ///< Recalculate our positions to track the anchor
private: private:
const QWidget* anchor; ///< The widget we're going to be tracking
QRect mainRect; QRect mainRect;
QPolygon spikePoly; QPolygon spikePoly;
QBrush brush; QBrush brush;
const int rectW, rectH; const int rectW, rectH;
const int spikeW, spikeH; const int spikeW, spikeH;
const int roundedFactor; const int roundedFactor; ///< By how much are the corners of the main rect rounded
}; };
#endif // CALLCONFIRMWIDGET_H #endif // CALLCONFIRMWIDGET_H

View File

@ -377,6 +377,8 @@ void Widget::resizeEvent(QResizeEvent *event)
{ {
Q_UNUSED(event); Q_UNUSED(event);
saveWindowGeometry(); saveWindowGeometry();
emit resized();
} }
QString Widget::detectProfile() QString Widget::detectProfile()

View File

@ -90,6 +90,7 @@ signals:
void usernameChanged(const QString& username); void usernameChanged(const QString& username);
void statusMessageChanged(const QString& statusMessage); void statusMessageChanged(const QString& statusMessage);
void changeProfile(const QString& profile); void changeProfile(const QString& profile);
void resized();
private slots: private slots:
void onConnected(); void onConnected();