You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
322 lines
9.5 KiB
322 lines
9.5 KiB
15 years ago
|
/**
|
||
|
* A class that lets the user draw with the mouse. The
|
||
|
* window knows how to redraw itself.
|
||
|
*/
|
||
|
|
||
|
import org.kde.qt.*;
|
||
|
|
||
13 years ago
|
public class TQTimerTest extends TQWidget {
|
||
15 years ago
|
|
||
|
public static final int COLOR_MENU_ID_BLACK = 0;
|
||
|
public static final int COLOR_MENU_ID_RED = 1;
|
||
|
public static final int COLOR_MENU_ID_BLUE = 2;
|
||
|
public static final int COLOR_MENU_ID_GREEN = 3;
|
||
|
public static final int COLOR_MENU_ID_YELLOW = 4;
|
||
|
|
||
|
public static int eventCount = 0;
|
||
|
|
||
13 years ago
|
private TQMenuBar _menubar;
|
||
|
private TQPopupMenu _filemenu;
|
||
|
private TQPopupMenu _colormenu;
|
||
|
private TQPopupMenu _helpmenu;
|
||
|
private TQScrollView _scrollview;
|
||
15 years ago
|
private ScribbleArea _scribblearea;
|
||
|
|
||
13 years ago
|
public class ScribbleArea extends TQWidget {
|
||
|
private TQPoint _last;
|
||
|
private TQColor _currentcolor;
|
||
15 years ago
|
|
||
13 years ago
|
private TQPixmap _buffer;
|
||
|
private TQPopupMenu _popupmenu;
|
||
15 years ago
|
|
||
|
/**
|
||
|
* The constructor. Initializes the member variables.
|
||
|
*/
|
||
|
ScribbleArea()
|
||
|
{
|
||
|
// initialize member variables
|
||
13 years ago
|
_buffer = new TQPixmap();
|
||
|
_last = new TQPoint();
|
||
15 years ago
|
_currentcolor = Qt.black();
|
||
|
|
||
|
// don't blank the window before repainting
|
||
|
setBackgroundMode( NoBackground );
|
||
|
|
||
|
// create a pop-up menu
|
||
13 years ago
|
_popupmenu = new TQPopupMenu();
|
||
15 years ago
|
_popupmenu.insertItem( "&Clear", this, Qt.SLOT( "slotClearArea()" ) );
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* This slot sets the curren color for the scribble area. It will be
|
||
13 years ago
|
* connected with the colorChanged( TQColor ) signal from the
|
||
15 years ago
|
* ScribbleWindow.
|
||
|
*/
|
||
13 years ago
|
public void setColor( TQColor new_color )
|
||
15 years ago
|
{
|
||
|
_currentcolor = new_color;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* This slot clears the drawing area by filling the off-screen buffer with
|
||
|
* white and copying it over to the window.
|
||
|
*/
|
||
|
public void slotClearArea()
|
||
|
{
|
||
|
// fill the off screen buffer with plain white
|
||
|
_buffer.fill( Qt.white() );
|
||
|
|
||
|
// and copy it over to the window
|
||
13 years ago
|
TQPaintDevice.bitBlt( this, 0, 0, _buffer );
|
||
15 years ago
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
13 years ago
|
* This method does the actual loading. It relies on TQPixmap (and the
|
||
15 years ago
|
* underlying I/O machinery) to determine the filetype.
|
||
|
*/
|
||
|
public void slotLoad( String filename )
|
||
|
{
|
||
|
if ( !_buffer.load( filename ) )
|
||
13 years ago
|
TQMessageBox.warning( null, "Load error", "Could not load file" );
|
||
15 years ago
|
|
||
|
repaint(); // refresh the window
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* This method does the actual saving. We hard-code the file type as
|
||
|
* BMP. Unix users might want to replace this with something like XPM.
|
||
|
*/
|
||
|
public void slotSave( String filename )
|
||
|
{
|
||
|
if( !_buffer.save( filename, "BMP" ) )
|
||
13 years ago
|
TQMessageBox.warning( null, "Save error", "Could not save file" );
|
||
15 years ago
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* This method is called whenever the user presses the
|
||
|
* mouse over the window. It just records the position of the mouse
|
||
|
* at the time of the click.
|
||
|
*/
|
||
13 years ago
|
public void mousePressEvent(TQMouseEvent event)
|
||
15 years ago
|
{
|
||
|
if ( event.button() == RightButton )
|
||
13 years ago
|
_popupmenu.exec( TQCursor.pos() );
|
||
15 years ago
|
else
|
||
|
{
|
||
|
_last = event.pos(); // retrieve the coordinates from the event
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* The method is called whenever the usr moves the mouse
|
||
|
* while the mouse button is pressed. If we had called
|
||
|
* setMouseTracking(true) before, the method would also be called
|
||
|
* when the mouse was moved with any button pressed. We know that
|
||
|
* we haven't, and thus don't have to check whether any buttons are
|
||
|
* pressed.
|
||
|
*/
|
||
13 years ago
|
public void mouseMoveEvent(TQMouseEvent event) {
|
||
|
// create a TQPainter object for drawing onto the window
|
||
|
TQPainter windowpainter = new TQPainter();
|
||
|
// and another TQPainter object for drawing int an off-screen pixmap
|
||
|
TQPainter bufferpainter = new TQPainter();
|
||
15 years ago
|
|
||
|
// start painting
|
||
|
windowpainter.begin( this ); // this painter paints onto the window
|
||
|
bufferpainter.begin( _buffer ); // and this one paints in the buffer
|
||
|
|
||
|
// set a standard pen with the currently selected color
|
||
|
windowpainter.setPen( _currentcolor );
|
||
|
bufferpainter.setPen( _currentcolor );
|
||
|
|
||
|
// draw a line in both the window and the buffer
|
||
|
windowpainter.drawLine( _last, event.pos() );
|
||
|
bufferpainter.drawLine( _last, event.pos() );
|
||
|
|
||
|
// done with painting
|
||
|
windowpainter.end();
|
||
|
bufferpainter.end();
|
||
|
|
||
|
// remember the current mouse position
|
||
|
_last = event.pos();
|
||
|
eventCount++;
|
||
|
}
|
||
|
/**
|
||
|
* This method is called whenever the widget needs
|
||
|
* painting, for example when it has been obscured and then revealed again.
|
||
|
*/
|
||
13 years ago
|
public void paintEvent(TQPaintEvent event) {
|
||
|
TQPaintDevice.bitBlt(this, 0, 0, _buffer);
|
||
15 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* This method get called whenever the widget needs
|
||
|
* painting, for example, when it has been obscured and then revealed again.
|
||
|
*/
|
||
13 years ago
|
public void resizeEvent(TQResizeEvent event) {
|
||
|
TQPixmap save = new TQPixmap( _buffer );
|
||
15 years ago
|
_buffer.resize( event.size() );
|
||
|
_buffer.fill( Qt.white() );
|
||
13 years ago
|
TQPaintDevice.bitBlt( _buffer, 0, 0, save );
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
13 years ago
|
TQTimerTest()
|
||
15 years ago
|
{
|
||
|
/* The next lines build the menu bar. We first create the menus
|
||
|
* one by one, then add them to the menu bar. */
|
||
13 years ago
|
_filemenu = new TQPopupMenu(); // create a file menu
|
||
15 years ago
|
_filemenu.insertItem( "&Load", this, Qt.SLOT( "slotLoad()" ) );
|
||
|
_filemenu.insertItem( "&Save", this, Qt.SLOT( "slotSave()" ) );
|
||
|
_filemenu.insertSeparator();
|
||
|
_filemenu.insertItem( "&Quit", Qt.qApp(), Qt.SLOT( "quit()" ) );
|
||
|
|
||
13 years ago
|
_colormenu = new TQPopupMenu(); // create a color menu
|
||
15 years ago
|
_colormenu.insertItem( "B&lack", COLOR_MENU_ID_BLACK);
|
||
|
_colormenu.insertItem( "&Red", COLOR_MENU_ID_RED);
|
||
|
_colormenu.insertItem( "&Blue", COLOR_MENU_ID_BLUE);
|
||
|
_colormenu.insertItem( "&Green", COLOR_MENU_ID_GREEN);
|
||
|
_colormenu.insertItem( "&Yellow", COLOR_MENU_ID_YELLOW);
|
||
13 years ago
|
TQObject.connect( _colormenu, SIGNAL( "activated(int)" ),
|
||
15 years ago
|
this, SLOT( "slotColorMenu(int)" ) );
|
||
|
|
||
13 years ago
|
_helpmenu = new TQPopupMenu(); // create a help menu
|
||
15 years ago
|
_helpmenu.insertItem( "&About QtScribble", this, SLOT( "slotAbout()" ) );
|
||
|
_helpmenu.insertItem( "&About Qt", this, SLOT( "slotAboutQt()" ) );
|
||
|
|
||
13 years ago
|
_menubar = new TQMenuBar( this, "" ); // create a menu bar
|
||
15 years ago
|
_menubar.insertItem( "&File", _filemenu );
|
||
|
_menubar.insertItem( "&Color", _colormenu );
|
||
|
_menubar.insertItem( "&Help", _helpmenu );
|
||
|
|
||
13 years ago
|
/* We create a TQScrollView and a ScribbleArea. The ScribbleArea will
|
||
15 years ago
|
* be managed by the scroll view.*/
|
||
13 years ago
|
_scrollview = new TQScrollView( this );
|
||
15 years ago
|
_scrollview.setGeometry( 0, _menubar.height(),
|
||
|
width(), height() - _menubar.height() );
|
||
|
_scribblearea = new ScribbleArea();
|
||
|
_scribblearea.setGeometry( 0, 0, 1000, 1000 );
|
||
|
_scrollview.addChild( _scribblearea );
|
||
13 years ago
|
TQObject.connect( this, SIGNAL( "colorChanged(TQColor)" ),
|
||
|
_scribblearea, SLOT( "setColor(TQColor)" ) );
|
||
|
TQObject.connect( this, SIGNAL( "save(java.lang.String)" ),
|
||
15 years ago
|
_scribblearea, SLOT( "slotSave(java.lang.String)" ) );
|
||
13 years ago
|
TQObject.connect( this, SIGNAL( "load(java.lang.String)" ),
|
||
15 years ago
|
_scribblearea, SLOT( "slotLoad(java.lang.String)" ) );
|
||
|
}
|
||
|
|
||
13 years ago
|
public void resizeEvent( TQResizeEvent event )
|
||
15 years ago
|
{
|
||
|
/* When the whole window is resized, we have to rearrange the geometry
|
||
|
* in the ScribbleWindow as well. Note that the ScribbleArea does not need
|
||
|
* to be changed. */
|
||
|
_scrollview.setGeometry( 0, _menubar.height(),
|
||
|
width(), height() - _menubar.height() );
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
public void slotAbout()
|
||
|
{
|
||
13 years ago
|
TQMessageBox.information( this, "About QtScribble 5",
|
||
15 years ago
|
"This is the Scribble 5 application\n" +
|
||
|
"Copyright 1998 by Mathias Kalle Dalheimer\n"
|
||
|
);
|
||
|
}
|
||
|
|
||
|
public void slotAboutQt()
|
||
|
{
|
||
13 years ago
|
TQMessageBox.aboutTQt( this, "About Qt" );
|
||
15 years ago
|
}
|
||
|
|
||
|
public void slotColorMenu( int item )
|
||
|
{
|
||
|
switch( item )
|
||
|
{
|
||
|
case COLOR_MENU_ID_BLACK:
|
||
|
emit("colorChanged", Qt.black());
|
||
|
break;
|
||
|
case COLOR_MENU_ID_RED:
|
||
|
emit("colorChanged", Qt.darkRed());
|
||
|
break;
|
||
|
case COLOR_MENU_ID_BLUE:
|
||
|
emit("colorChanged", Qt.darkBlue());
|
||
|
break;
|
||
|
case COLOR_MENU_ID_GREEN:
|
||
|
emit("colorChanged", Qt.darkGreen());
|
||
|
break;
|
||
|
case COLOR_MENU_ID_YELLOW:
|
||
|
emit("colorChanged", Qt.yellow());
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* This is the slot for the menu item File/Load. It opens a
|
||
13 years ago
|
* TQFileDialog to ask the user for a filename, then emits a save()
|
||
15 years ago
|
* signal with the filename as parameter.
|
||
|
*/
|
||
|
public void slotLoad()
|
||
|
{
|
||
|
/* Open a file dialog for loading. The default directory is the
|
||
|
* current directory, the filter *.bmp.
|
||
|
*/
|
||
13 years ago
|
String filename = TQFileDialog.getOpenFileName( ".", "*.bmp", this );
|
||
15 years ago
|
if ( !filename.equals("") )
|
||
|
emit("load", filename);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* This is the slot for the menu item File/Load. It opens a
|
||
13 years ago
|
* TQFileDialog to ask the user for a filename, then emits a save()
|
||
15 years ago
|
* signal with the filename as parameter.
|
||
|
*/
|
||
|
public void slotSave()
|
||
|
{
|
||
|
/* Open a file dialog for saving. The default directory is the
|
||
|
* current directory, the filter *.bmp.
|
||
|
*/
|
||
13 years ago
|
String filename = TQFileDialog.getSaveFileName( ".", "*.bmp", this );
|
||
15 years ago
|
if ( !filename.equals("") )
|
||
|
emit("save", filename);
|
||
|
}
|
||
|
|
||
|
public void timerDone()
|
||
|
{
|
||
|
// System.out.println("in timerDone...");
|
||
|
}
|
||
|
|
||
|
public static void main(String[] args)
|
||
|
{
|
||
13 years ago
|
TQApplication myapp = new TQApplication(args);
|
||
|
TQTimerTest mywidget = new TQTimerTest();
|
||
15 years ago
|
mywidget.setGeometry(50, 500, 400, 400);
|
||
|
|
||
13 years ago
|
TQTimer timer = new TQTimer( mywidget );
|
||
15 years ago
|
connect( timer, Qt.SIGNAL("timeout()"),
|
||
|
mywidget, Qt.SLOT("timerDone()") );
|
||
|
timer.start( 2000, false ); // 2 seconds single-shot
|
||
|
|
||
|
myapp.setMainWidget(mywidget);
|
||
|
|
||
|
AppWizardBase mywizard = new AppWizardBase(mywidget, "", false, 0);
|
||
|
mywizard.show();
|
||
|
FilePropsPageBase mypropspage = new FilePropsPageBase(mywidget, "", 0);
|
||
|
mypropspage.show();
|
||
|
|
||
|
mywidget.show();
|
||
|
myapp.exec();
|
||
|
System.out.println("event count: " + eventCount);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
static {
|
||
|
qtjava.initialize();
|
||
|
}
|
||
|
}
|