goldendict-ng/src/ui/scanflag.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

90 lines
2 KiB
C++
Raw Normal View History

#include "scanflag.hh"
#include <QCursor>
#include <QGuiApplication>
#include <QScreen>
ScanFlag::ScanFlag( QWidget * parent ):
QMainWindow( parent ),
pushButton( new QPushButton( this ) )
{
pushButton->setIcon( QIcon( ":/icons/programicon.png" ) );
setCentralWidget( pushButton );
setFixedSize( 30, 30 );
setWindowFlags( Qt::ToolTip | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::WindowDoesNotAcceptFocus );
setAttribute( Qt::WA_TranslucentBackground );
setAttribute( Qt::WA_X11DoNotAcceptFocus );
hideTimer.setSingleShot( true );
hideTimer.setInterval( 1000 );
connect( &hideTimer, &QTimer::timeout, this, &ScanFlag::hideWindow );
connect( pushButton, &QPushButton::clicked, this, &ScanFlag::pushButtonClicked );
}
void ScanFlag::pushButtonClicked()
{
hideTimer.stop();
hide();
emit requestScanPopup();
}
void ScanFlag::hideWindow()
{
if ( isVisible() )
hide();
}
void ScanFlag::showScanFlag()
{
if ( isVisible() )
hide();
QPoint currentPos = QCursor::pos();
auto screen = QGuiApplication::screenAt( currentPos );
if ( !screen )
return;
QRect desktop = screen->geometry();
QSize windowSize = geometry().size();
int x, y;
/// Try the to-the-right placement
2023-05-13 14:06:36 +00:00
if ( currentPos.x() + 4 + windowSize.width() <= desktop.topRight().x() ) {
x = currentPos.x() + 4;
2023-05-13 14:06:36 +00:00
}
else if ( currentPos.x() - 4 - windowSize.width() >= desktop.x() ) {
/// Try the to-the-left placement
x = currentPos.x() - 4 - windowSize.width();
2023-05-13 14:06:36 +00:00
}
else { // Center it
x = desktop.x() + ( desktop.width() - windowSize.width() ) / 2;
2023-05-13 14:06:36 +00:00
}
/// Try the to-the-top placement
2023-05-13 14:06:36 +00:00
if ( currentPos.y() - 15 - windowSize.height() >= desktop.y() ) {
y = currentPos.y() - 15 - windowSize.height();
2023-05-13 14:06:36 +00:00
}
else if ( currentPos.y() + 15 + windowSize.height() <= desktop.bottomLeft().y() ) {
/// Try the to-the-bottom placement
y = currentPos.y() + 15;
2023-05-13 14:06:36 +00:00
}
else { // Center it
y = desktop.y() + ( desktop.height() - windowSize.height() ) / 2;
2023-05-13 14:06:36 +00:00
}
move( x, y );
show();
hideTimer.start();
}