summaryrefslogtreecommitdiffstats
path: root/qtjava/javalib/examples/popup/Frame.java
blob: afbf2edc992940e9f533252048bf6f071ac102c8 (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
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
/***************************************************************************
* $Id$
**
* Definition of something or other
**
* Created : 979899
**
* Copyright (C) 1997 by Trolltech AS.  All rights reserved.
**
* This file is part of an example program for Qt.  This example
* program may be used, distributed and modified without limitation.
**
****************************************************************************/

import org.kde.qt.*;

class Frame extends QFrame
{

private     QPushButton button1;
private     QPushButton button2;
     
private     QFrame popup1;
private     FancyPopup popup2;

Frame()
{
	this(null, null);
}

Frame(QWidget parent, String name)
{
    super(parent, name);
    button1 = new QPushButton("Simple Popup", this);
    connect ( button1, SIGNAL(" clicked()"), SLOT(" button1Clicked()") );
    button2 = new QPushButton("Fancy Popup", this);
    connect ( button2, SIGNAL(" pressed()"), SLOT(" button2Pressed()") );

    QBoxLayout  l = new QHBoxLayout( this );
    button1.setMaximumSize(button1.sizeHint());
    button2.setMaximumSize(button2.sizeHint());
    l.addWidget( button1 );
    l.addWidget( button2 );
    l.activate();

//     button1.setGeometry(20,20,100,30);
//     button2.setGeometry(140,20,100,30);
    resize(270, 70);

    //create a very simple popup: it is just composed with other
    //widget and will be shown after clicking on button1

    popup1 = new QFrame( this ,null, WType_Popup);
    popup1.setFrameStyle( WinPanel|Raised );
    popup1.resize(150,100);
    QLineEdit tmpE = new QLineEdit( popup1 );
    connect( tmpE, SIGNAL(" returnPressed()"), popup1, SLOT(" hide()") );
    tmpE.setGeometry(10,10, 130, 30);
    tmpE.setFocus();
    QPushButton tmpB = new QPushButton("Click me!", popup1);
    connect( tmpB, SIGNAL(" clicked()"), popup1, SLOT(" close()") );
    tmpB.setGeometry(10, 50, 130, 30);

    // the fancier version uses its own class. It will be shown when
    // pressing button2, so they behavior is more like a modern menu
    // or toolbar.

   popup2 = new FancyPopup( this );

    // you might also add new widgets to the popup, just like you do
    // it with any other widget.  The next four lines (if not
    // commented out) will for instance add a line edit widget.

//     tmpE = new QLineEdit( popup2 );
//     tmpE.setFocus();
//     connect( tmpE, SIGNAL(" returnPressed()"), popup2, SLOT(" close()") );
//     tmpE.setGeometry(10, 10, 130, 30);
}


void button1Clicked(){
    popup1.move( mapToGlobal( button1.geometry().bottomLeft() ) );
    popup1.show();
}

void button2Pressed(){
    popup2.popup(button2);
}

class FancyPopup  extends QLabel
{
private    QWidget popupParent;
private    int moves;



FancyPopup( QWidget parent )
{
	this(parent, null);
}

FancyPopup( QWidget parent, String  name )
{
    super( parent, name, WType_Popup );
        setFrameStyle( WinPanel|Raised );
        setAlignment( AlignCenter );
        resize(150,100);
        moves = 0;
        setMouseTracking( true );
}

protected void mouseMoveEvent( QMouseEvent  e){
    moves++;
    String s = e.pos().x() + "/" + e.pos().y();
    if ((e.state() & QMouseEvent.LeftButton) != 0)
        s += " (down)";
    setText(s);
}

protected void mouseReleaseEvent( QMouseEvent  e){
    if  (rect().contains( e.pos() ) || moves > 5)
        close();
}

protected void closeEvent( QCloseEvent e ){
    e.accept();
    moves = 0;
    if (popupParent == null)
        return;

    // remember that we (as a popup) might recieve the mouse release
    // event instead of the popupParent. This is due to the fact that
    // the popupParent popped us up in its mousePressEvent handler. To
    // avoid the button remaining in pressed state we simply send a
    // faked mouse button release event to it.
    QMouseEvent me = new QMouseEvent( QEvent.MouseButtonRelease, new QPoint(0,0), new QPoint(0,0), QMouseEvent.LeftButton, QMouseEvent.NoButton);
    QApplication.sendEvent( popupParent, me );
}

void popup( QWidget parent) {
    popupParent = parent;
    setText("Move the mouse!");
    if (popupParent != null)
        move( popupParent.mapToGlobal( popupParent.rect().bottomLeft() ) );
    show();
}

}


public static void main(String[] args)
{
    QApplication a = new QApplication( args );

    Frame frame = new Frame();
    frame.setCaption("Qt Example - Custom Popups");
    a.setMainWidget(frame);
    frame.show();
    a.exec();
    return;
}

	static {
		qtjava.initialize();
	}
}