File: QtUtil.cpp

package info (click to toggle)
qtop 2.3.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,976 kB
  • sloc: cpp: 40,477; makefile: 7
file content (236 lines) | stat: -rw-r--r-- 7,605 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/******************************************************************************
*
* Copyright (C) 2002 Hugo PEREIRA <mailto: hugo.pereira@free.fr>
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* Any WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program.  If not, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/

#include "QtUtil.h"

#include "Debug.h"
#include "Util.h"

#include <QApplication>
#include <QCursor>
#include <QDesktopWidget>
#include <QFontMetrics>
#include <QLayout>
#include <QMessageBox>
#include <QPushButton>
#include <QStyleOption>

//____________________________________________________________
void QtUtil::moveWidget( QWidget* widget, QPoint position )
{

    Debug::Throw( "QtUtil::moveWidget.\n" );
    if( !widget ) return;
    QDesktopWidget* desktop( qApp->desktop() );
    QRect geometry( desktop->screenGeometry( desktop->screenNumber( widget ) ) );
    if( position.y() + widget->height() > geometry.bottom()+1 ) position.setY( geometry.bottom() + 1 - widget->height() );

    widget->move( position );
}

//____________________________________________________________
QPoint QtUtil::centerOnPointer( const QSize& size )
{
    Debug::Throw( "QtUtil::centerOnPointer.\n" );

    // get cursor position
    QPoint point( QCursor::pos() );

    point.setX( point.x() - size.width()/2 );
    point.setY( point.y() - size.height()/2 );

    // retrieve desktop
    QDesktopWidget *desktop( qApp->desktop() );

    // check point against desktop size
    if( point.x() + size.width()> desktop->width() ) point.setX( desktop->width() - size.width() );
    if( point.y() + size.height()> desktop->height() ) point.setY( desktop->height() - size.height() );

    // check point against (0,0)
    if( point.x() < 0 ) point.setX( 0 );
    if( point.y() < 0 ) point.setY( 0 );
    return point;
}


//____________________________________________________________
QPoint QtUtil::centerOnWidget( const QSize& size, QWidget* widget )
{
    Debug::Throw( "QtUtil::centerOnWidget.\n" );
    if( !widget ) return centerOnDesktop( size );

    Debug::Throw() << "QtUtil::centerOnWidget - size: (" << size.width() << "," << size.height() << ")" << endl;

    // get parent position and size
    QPoint point( widget->pos() );
    if( widget != widget->window() ) point = widget->mapToGlobal( point );

    QSize parent_size( widget->frameSize() );

    Debug::Throw() << "QtUtil::centerOnWidget - parent size: (" << parent_size.width() << "," << parent_size.height() << ")" << endl;
    Debug::Throw() << "QtUtil::centerOnWidget - parent position: (" << point.x() << "," << point.y() << ")" << endl;

    point.setX( point.x() + ( parent_size.width() - size.width() )/2 );
    point.setY( point.y() + ( parent_size.height() - size.height() )/2 );

    // retrieve desktop
    QDesktopWidget *desktop( qApp->desktop() );

    // check point against desktop size
    if( point.x() + size.width()> desktop->width() ) point.setX( desktop->width() - size.width() );
    if( point.y() + size.height()> desktop->height() ) point.setY( desktop->height() - size.height() );

    // check point against (0,0)
    if( point.x() < 0 ) point.setX( 0 );
    if( point.y() < 0 ) point.setY( 0 );
    return point;

}

//____________________________________________________________
QPoint QtUtil::centerOnDesktop( const QSize& size )
{
    Debug::Throw( "QtUtil::centerOnDesktop.\n" );

    // retrieve desktop
    QDesktopWidget *desktop( qApp->desktop() );
    QSize desktop_size( desktop->frameSize() );

    // get parent position and size
    QPoint point( desktop->pos() );

    point.setX( point.x() + ( desktop_size.width() - size.width() )/2 );
    point.setY( point.y() + ( desktop_size.height() - size.height() )/2 );

    // check point against desktop size
    if( point.x() + size.width()> desktop->width() ) point.setX( desktop->width() - size.width() );
    if( point.y() + size.height()> desktop->height() ) point.setY( desktop->height() - size.height() );

    // check point against (0,0)
    if( point.x() < 0 ) point.setX( 0 );
    if( point.y() < 0 ) point.setY( 0 );
    return point;
}

//____________________________________________________________
QWidget* QtUtil::centerOnPointer( QWidget* widget )
{
    Debug::Throw( "QtUtil::centerOnPointer.\n" );
    Q_CHECK_PTR( widget );

    // move widget
    widget->move( centerOnPointer( widget->frameSize() ) );
    qApp->processEvents();
    return widget;
}

//____________________________________________________________
QWidget* QtUtil::centerOnParent( QWidget* widget )
{
    Q_CHECK_PTR( widget );
    return centerOnWidget( widget, widget->parentWidget() );
}


//____________________________________________________________
QWidget* QtUtil::centerOnWidget( QWidget* widget, QWidget* parent )
{
    Debug::Throw( "QtUtil::centerOnParent.\n" );
    Q_CHECK_PTR( widget );

    // get parent widget
    if( !( parent && parent->window() ) ) centerOnDesktop( widget );
    else widget->move( centerOnWidget( widget->frameSize(), parent->window() ) );
    return widget;
}


//____________________________________________________________
QWidget* QtUtil::centerOnDesktop( QWidget* widget )
{
    Debug::Throw( "QtUtil::centerOnDesktop.\n" );
    Q_CHECK_PTR( widget );
    widget->move( centerOnDesktop( widget->frameSize() ) );
    return widget;
}

//____________________________________________________________
QWidget* QtUtil::uniconify( QWidget *widget )
{

    Debug::Throw( "QtUtil::uniconify.\n" );

    if( !widget->isTopLevel() ) return widget;
    widget = widget->window();

    if( widget->isMinimized() ) widget->showNormal();
    else if( widget->isHidden() ) widget->show();

    widget->activateWindow();
    widget->raise();

    return widget;

}

//____________________________________________________________
QFont QtUtil::titleFont( QFont font )
{
    font.setPointSize( qRound(font.pointSize() * 1.4) );
    return font;
}

//____________________________________________________________
QAction* QtUtil::addMenuSection( QMenu* menu, const QIcon& icon, const QString& text )
{
    menu->setSeparatorsCollapsible( false );

    #if QT_VERSION >= 0x050000
    QAction* action = menu->addSection( icon, text );
    #else
    QAction* action = menu->addSeparator();
    if( !text.isEmpty() ) action->setText( text );
    if( !icon.isNull() ) action->setIcon( icon );
    #endif

    // calculate minimum size, needed to properly account for header
    int width = 0;
    if( !icon.isNull() )
    {
        QStyleOption opt;
        opt.init( menu );
        const int iconSize = menu->style()->pixelMetric(QStyle::PM_SmallIconSize, &opt, menu);
        width += iconSize;
    }

    if( !text.isNull() )
    {
        QFont font( menu->font() );
        font.setWeight( QFont::Bold );
        width += QFontMetrics( font ).width( text ) + 24;
    }

    if( width > 0 )
    {
        QSize minSizeHint( menu->minimumSizeHint() );
        menu->setMinimumSize( QSize( qMax( width, minSizeHint.width() ), qMax( 0, minSizeHint.height() ) ) );
    }

    return action;
}