summaryrefslogtreecommitdiffstats
path: root/qtjava/javalib/examples/mdi/MDIWindow.java
blob: ef5efd649944363f69b2d02e7f8835ff0c3fedde (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
/***************************************************************************
* $Id$
**
* Copyright (C) 1992-2000 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 MDIWindow extends QMainWindow
{

private    QMultiLineEdit medit;
private    QMovie  mmovie;
private    String filename = "";

MDIWindow( QWidget parent, String name, int wflags )
{
    super( parent, name, wflags );
    mmovie = null;
    medit = new QMultiLineEdit( this );
    setFocusProxy( medit );
    setCentralWidget( medit );
}


void load( String fn )
{
    filename  = fn;
    QFile f = new QFile( filename );
    if ( !f.open( QIODevice.IO_ReadOnly ) )
	return;

    if(fn.indexOf(".gif") != -1) {
	QWidget  tmp=new QWidget(this);
	setFocusProxy(tmp);
	setCentralWidget(tmp);
	medit.hide();
	QMovie  qm=new QMovie(fn);
//	qm.setDisplayWidget(tmp);
	tmp.setBackgroundMode(QWidget.NoBackground);
	tmp.show();
	mmovie=qm;
    } else {
	mmovie = null;

	QTextStream t = new QTextStream(f);
	String s = t.read();
	medit.setText( s );
	f.close();


    }
    setCaption( filename );
    emit("message", "Loaded document " + filename, 2000 );
}

void save()
{
    if ( filename.equals("") ) {
        saveAs();
        return;
    }

    String text = medit.text();
    QFile f = new QFile( filename );
    if ( !f.open( QIODevice.IO_WriteOnly ) ) {
        emit("message", "Could not write to " + filename,
		      2000 );
        return;
    }

    QTextStream t = new QTextStream( f );
	t.writeRawBytes( text, text.length() );
    f.close();

    setCaption( filename );

    emit("message", "File " + filename + " saved", 2000 );
}

void saveAs()
{
    String fn = QFileDialog.getSaveFileName( filename, "", this );
    if ( !fn.equals("") ) {
        filename = fn;
        save();
    } else {
        emit("message", "Saving aborted", 2000 );
    }
}

void print( QPrinter printer)
{
    int Margin = 10;
    int pageNo = 1;

    if ( printer.setup(this) ) {		// printer dialog
	emit("message", "Printing...", 0 );
	QPainter p = new QPainter();
	if ( !p.begin( printer ) )
	    return;				// paint on printer
	p.setFont( medit.font() );
	int yPos        = 0;			// y position for each line
	QFontMetrics fm = p.fontMetrics();
	QPaintDeviceMetrics metrics = new QPaintDeviceMetrics( printer ); // need width/height
	// of printer surface
	for( int i = 0 ; i < medit.numLines() ; i++ ) {
	    if ( Margin + yPos > metrics.height() - Margin ) {
		String msg = "Printing (page ";
		msg += (++pageNo);
		msg += ")...";
		emit("message", msg, 0 );
		printer.newPage();		// no more room on this page
		yPos = 0;			// back to top of page
	    }
	    p.drawText( Margin, Margin + yPos,
			metrics.width(), fm.lineSpacing(),
			ExpandTabs | DontClip,
			medit.textLine( i ) );
	    yPos = yPos + fm.lineSpacing();
	}
	p.end();				// send job to printer
	emit("message", "Printing completed", 2000 );
    } else {
	emit("message", "Printing aborted", 2000 );
    }
}

}