summaryrefslogtreecommitdiffstats
path: root/src/popupmenu.h
blob: fa92f39e4092833f6483e600597e565469634a78 (plain)
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
/***************************************************************************
 *   Copyright (C) 2003 by Sébastien Laoût                                 *
 *   slaout@linux62.org                                                    *
 *                                                                         *
 *   This program 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 program 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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#ifndef POPUPMENU_H
#define POPUPMENU_H

class QPopupMenu;
class QRect;

/** QPopupMenu/KPopupMenu doesn't provide metod to exec a menu
  * at a given rectangle !
  * eg, popup at bottom of a rectangle, and at top if not possible...
  * @author Sébastien Laoût
  */
namespace PopupMenu
{
	/** Show the popup menu centered into rect.
	  */
	void execAtRectCenter(QPopupMenu &menu, const QRect &rect);

	/** Show the popup menu at left-bottom of rect, or at right-bottom
	  * if not possible (not enought place).
	  * If it isn't possible to show it at bottom, it will be shown on
	  * top of rect (top-left if possible, if not it will be top-right).
	  * If center is true, it will try to horizontaly center the popup with
	  * rect, so it will try two positions : bottom center and then top center.
	  */
	void execAtRectBottom(QPopupMenu &menu, const QRect &rect, bool centered = false);

	/** Idem execAtRectBottom but on the right or left sides,
	  * prior aligned with the top of the rect, and at the bottom
	  * if not possible.
	  * If center is true, it will try to vertically center the popup with
	  * rect, so it will try two positions : right center and then left center.
	  */
	void execAtRectRight(QPopupMenu &menu, const QRect &rect, bool centered = false);
}

/** Test window of PopupMenu methods.
  * Just include popupmenu.h in a main Qt application and call
  * new PopupMenuTest();
  * Click the window for more explications.
  * Resize it to test particular cases.
  * (Comment the class, if it isn't done yet to do not compile it :-) ).
  * @author Sébastien Laoût
  */

/*****

#include <qwidget.h>
#include <qpopupmenu.h>
#include <qpainter.h>
#include <qpen.h>

c l a s s   P o p u p M e n u T e s t   :   p u b l i c   Q W i d g e t
{
  Q _ O B J E C T
  p u b l i c:
	PopupMenuTest()
	 : QWidget(0)
	{
		setCaption("Click to test!");
		show();
	}

	void mousePressEvent(QMouseEvent *event)
	{
		QPopupMenu menu;
		QRect rect( mapToGlobal(QPoint(0,0)), size() );

		menu.insertItem("A test of popup menu!");
		menu.insertItem("This menu contain some items");
		menu.insertItem("Resize the window as you want and:");
		menu.insertItem("- click : execAtRectCenter");
		menu.insertItem("- right click : execAtRectBottom");
		menu.insertItem("- middle click : execAtRectRight");
		menu.insertItem("- Shift + right click : execAtRectBottom centered");
		menu.insertItem("- Shift + middle click : execAtRectRight centered");

		if (event->button() & Qt::LeftButton)
			PopupMenu::execAtRectCenter(menu, rect);
		else if ((event->button() & Qt::RightButton) && (event->state() & Qt::ShiftButton))
			PopupMenu::execAtRectBottom(menu, rect, true);
		else if (event->button() & Qt::RightButton)
			PopupMenu::execAtRectBottom(menu, rect);
		else if ((event->button() & Qt::MidButton) && (event->state() & Qt::ShiftButton))
			PopupMenu::execAtRectRight(menu, rect, true);
		else if (event->button() & Qt::MidButton)
			PopupMenu::execAtRectRight(menu, rect);
	}

	void paintEvent(QPaintEvent*)
	{
		QPainter paint(this);
		paint.setPen(paletteBackgroundColor());
		paint.drawRect(rect());
		paint.drawWinFocusRect(rect());
		paint.setPen( QPen(Qt::black, 1) );
		paint.drawLine( rect().topLeft(), rect().bottomRight() );
		paint.drawLine( rect().topRight(), rect().bottomLeft() );
	}
};

*****/

#endif // POPUPMENU_H