2009-04-12 11:21 UTC-0800 Pritpal Bedi (pritpal@vouchcac.com)
* harbour/contrib/gtqtc/gtqtc.cpp
* harbour/contrib/gtqtc/gtqtc.h
* harbour/contrib/gtqtc/Makefile
! Few more optimizations.
- harbour/contrib/gtqtc/qtconsolearea.cpp
- harbour/contrib/gtqtc/qtmainwindow.cpp
- Removed. Needed to have better control over the operations.
This commit is contained in:
@@ -8,6 +8,16 @@
|
||||
2009-12-31 13:59 UTC+0100 Foo Bar (foo.bar foobar.org)
|
||||
*/
|
||||
|
||||
2009-04-12 11:21 UTC-0800 Pritpal Bedi (pritpal@vouchcac.com)
|
||||
* harbour/contrib/gtqtc/gtqtc.cpp
|
||||
* harbour/contrib/gtqtc/gtqtc.h
|
||||
* harbour/contrib/gtqtc/Makefile
|
||||
! Few more optimizations.
|
||||
|
||||
- harbour/contrib/gtqtc/qtconsolearea.cpp
|
||||
- harbour/contrib/gtqtc/qtmainwindow.cpp
|
||||
- Removed. Needed to have better control over the operations.
|
||||
|
||||
2009-04-12 08:57 UTC-0800 Pritpal Bedi (pritpal@vouchcac.com)
|
||||
* harbour/contrib/gtqtc/gtqtc.cpp
|
||||
* harbour/contrib/gtqtc/qtconsolearea.cpp
|
||||
|
||||
@@ -56,8 +56,6 @@ HB_USER_CFLAGS += $(foreach d, $(HB_INC_QT_OK), -I$(d))
|
||||
CPP_SOURCES=\
|
||||
gtqtc.cpp \
|
||||
moc_gtqtc.cpp \
|
||||
qtconsolearea.cpp \
|
||||
qtmainwindow.cpp \
|
||||
|
||||
|
||||
C_HEADERS=\
|
||||
|
||||
@@ -2562,3 +2562,430 @@ HB_CALL_ON_STARTUP_END( _hb_startup_gt_Init_ )
|
||||
|
||||
/* ********************************************************************** */
|
||||
|
||||
ConsoleArea::ConsoleArea(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
{
|
||||
setAttribute(Qt::WA_StaticContents);
|
||||
modified = false;
|
||||
scribbling = false;
|
||||
myPenWidth = 1;
|
||||
myPenColor = Qt::blue;
|
||||
|
||||
COLORS[ 0] = BLACK;
|
||||
COLORS[ 1] = BLUE;
|
||||
COLORS[ 2] = GREEN;
|
||||
COLORS[ 3] = CYAN;
|
||||
COLORS[ 4] = RED;
|
||||
COLORS[ 5] = MAGENTA;
|
||||
COLORS[ 6] = BROWN;
|
||||
COLORS[ 7] = WHITE;
|
||||
COLORS[ 8] = LIGHT_GRAY;
|
||||
COLORS[ 9] = BRIGHT_BLUE;
|
||||
COLORS[10] = BRIGHT_GREEN;
|
||||
COLORS[11] = BRIGHT_CYAN;
|
||||
COLORS[12] = BRIGHT_RED;
|
||||
COLORS[13] = BRIGHT_MAGENTA;
|
||||
COLORS[14] = YELLOW;
|
||||
COLORS[15] = BRIGHT_WHITE;
|
||||
|
||||
setFocusPolicy(Qt::StrongFocus);
|
||||
// grabKeyboard();
|
||||
}
|
||||
|
||||
void ConsoleArea::sizeByFont(void)
|
||||
{
|
||||
QPainter painter( this );
|
||||
qFont = QFont( tr("Courier New"), 12, -1, FALSE );
|
||||
qFont = QFont( qFont, painter.device() );
|
||||
QFontMetrics fontMetrics( qFont );
|
||||
fontHeight = fontMetrics.height();
|
||||
fontWidth = fontMetrics.averageCharWidth();
|
||||
resize( fontWidth*80, fontHeight*25 );
|
||||
}
|
||||
|
||||
bool ConsoleArea::openImage(const QString &fileName)
|
||||
{
|
||||
QImage loadedImage;
|
||||
if (!loadedImage.load(fileName))
|
||||
return false;
|
||||
|
||||
QSize newSize = loadedImage.size().expandedTo(size());
|
||||
resizeImage(&loadedImage, newSize);
|
||||
image = loadedImage;
|
||||
modified = false;
|
||||
update();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ConsoleArea::saveImage(const QString &fileName, const char *fileFormat)
|
||||
{
|
||||
QImage visibleImage = image;
|
||||
resizeImage(&visibleImage, size());
|
||||
|
||||
if (visibleImage.save(fileName, fileFormat)) {
|
||||
modified = false;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void ConsoleArea::setPenColor(const QColor &newColor)
|
||||
{
|
||||
myPenColor = newColor;
|
||||
}
|
||||
|
||||
void ConsoleArea::setPenWidth(int newWidth)
|
||||
{
|
||||
myPenWidth = newWidth;
|
||||
}
|
||||
|
||||
void ConsoleArea::clearImage()
|
||||
{
|
||||
image.fill(qRgb(255, 255, 255));
|
||||
modified = true;
|
||||
update();
|
||||
}
|
||||
|
||||
void ConsoleArea::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
if (event->button() == Qt::LeftButton) {
|
||||
lastPoint = event->pos();
|
||||
scribbling = true;
|
||||
}
|
||||
}
|
||||
|
||||
void ConsoleArea::keyPressEvent(QKeyEvent *event)
|
||||
{
|
||||
OutputDebugString( "Key Pressed" );
|
||||
}
|
||||
|
||||
void ConsoleArea::keyReleaseEvent(QKeyEvent *event)
|
||||
{
|
||||
OutputDebugString( "Key Released" );
|
||||
}
|
||||
|
||||
void ConsoleArea::mouseMoveEvent(QMouseEvent *event)
|
||||
{
|
||||
if ((event->buttons() & Qt::LeftButton) && scribbling)
|
||||
drawLineTo(event->pos());
|
||||
}
|
||||
|
||||
void ConsoleArea::mouseReleaseEvent(QMouseEvent *event)
|
||||
{
|
||||
if (event->button() == Qt::LeftButton && scribbling) {
|
||||
drawLineTo(event->pos());
|
||||
scribbling = false;
|
||||
}
|
||||
}
|
||||
|
||||
void ConsoleArea::paintEvent(QPaintEvent * event)
|
||||
{
|
||||
OutputDebugString( " entry" );
|
||||
QPainter painter(this);
|
||||
QRect rect = event->rect();
|
||||
//painter.drawImage(QPoint(0, 0), image);
|
||||
{
|
||||
int hgt = height();
|
||||
int fntHeight = (hgt/25);
|
||||
fntHeight = 14;
|
||||
QFont font( tr( "Courier New" ), fntHeight, 10, FALSE );
|
||||
font = QFont(font, painter.device());
|
||||
QFontMetrics fontMetrics( font );
|
||||
int height = fontMetrics.height();
|
||||
painter.setFont(font);
|
||||
painter.setBackgroundMode(Qt::OpaqueMode);
|
||||
{
|
||||
int iRow,iCol,startCol,len ;
|
||||
int iTop;
|
||||
USHORT usChar;
|
||||
BYTE bColor, bAttr, bOldColor = 0;
|
||||
char text[ 4000 ];
|
||||
|
||||
iTop = 0;
|
||||
for( iRow = 0; iRow <= 24; iRow++ )
|
||||
{
|
||||
iCol = startCol = 0;
|
||||
len = 0;
|
||||
iTop = ( iRow * height ) + height;
|
||||
|
||||
while( iCol <= 79 )
|
||||
{
|
||||
if( !HB_GTSELF_GETSCRCHAR( pGT, iRow, iCol, &bColor, &bAttr, &usChar ) )
|
||||
break;
|
||||
|
||||
if( len == 0 )
|
||||
{
|
||||
bOldColor = bColor;
|
||||
}
|
||||
else if( bColor != bOldColor )
|
||||
{
|
||||
QPen pen( COLORS[ bOldColor & 0x0F ] );
|
||||
painter.setPen( pen );
|
||||
QBrush brush( COLORS[ bOldColor >> 4 ] );
|
||||
painter.setBackground( brush );
|
||||
|
||||
OutputDebugString( "KKKK len=%i bOldColor=%i bColor=%i" );
|
||||
|
||||
painter.drawText( QPointF( startCol,iTop ), QString( text ) );
|
||||
|
||||
bOldColor = bColor;
|
||||
startCol = iCol;
|
||||
len = 0;
|
||||
}
|
||||
text[ len++ ] = ( char ) usChar;
|
||||
iCol++;
|
||||
}
|
||||
if( len > 0 )
|
||||
{
|
||||
QPen pen( COLORS[ bOldColor & 0x0F ] );
|
||||
painter.setPen( pen );
|
||||
QBrush brush( COLORS[ bOldColor >> 4 ] );
|
||||
painter.setBackground( brush );
|
||||
//OutputDebugString( "mmmmmm len=%i bOldColor=%i bColor=%i" );
|
||||
painter.drawText(QPointF(startCol,iTop),QString(text));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
OutputDebugString( " exit" );
|
||||
}
|
||||
|
||||
void ConsoleArea::resizeEvent(QResizeEvent *event)
|
||||
{
|
||||
if (width() > image.width() || height() > image.height()) {
|
||||
int newWidth = qMax(width() + 128, image.width());
|
||||
int newHeight = qMax(height() + 128, image.height());
|
||||
resizeImage(&image, QSize(newWidth, newHeight));
|
||||
update();
|
||||
}
|
||||
QWidget::resizeEvent(event);
|
||||
}
|
||||
|
||||
void ConsoleArea::drawLineTo(const QPoint &endPoint)
|
||||
{
|
||||
QPainter painter(&image);
|
||||
painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap,
|
||||
Qt::RoundJoin));
|
||||
painter.drawLine(lastPoint, endPoint);
|
||||
modified = true;
|
||||
|
||||
int rad = (myPenWidth / 2) + 2;
|
||||
update(QRect(lastPoint, endPoint).normalized().adjusted(-rad, -rad, +rad, +rad));
|
||||
lastPoint = endPoint;
|
||||
}
|
||||
|
||||
void ConsoleArea::resizeImage(QImage *image, const QSize &newSize)
|
||||
{
|
||||
if (image->size() == newSize)
|
||||
return;
|
||||
|
||||
QImage newImage(newSize, QImage::Format_RGB32);
|
||||
newImage.fill(qRgb(255, 255, 255));
|
||||
QPainter painter(&newImage);
|
||||
painter.drawImage(QPoint(0, 0), *image);
|
||||
*image = newImage;
|
||||
}
|
||||
|
||||
void ConsoleArea::print()
|
||||
{
|
||||
#ifndef QT_NO_PRINTER
|
||||
QPrinter printer(QPrinter::HighResolution);
|
||||
|
||||
QPrintDialog *printDialog = new QPrintDialog(&printer, this);
|
||||
if (printDialog->exec() == QDialog::Accepted) {
|
||||
QPainter painter(&printer);
|
||||
QRect rect = painter.viewport();
|
||||
QSize size = image.size();
|
||||
size.scale(rect.size(), Qt::KeepAspectRatio);
|
||||
painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
|
||||
painter.setWindow(image.rect());
|
||||
painter.drawImage(0, 0, image);
|
||||
}
|
||||
#endif // QT_NO_PRINTER
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------*/
|
||||
/*
|
||||
*
|
||||
*
|
||||
/*----------------------------------------------------------------------*/
|
||||
|
||||
MainWindow::MainWindow()
|
||||
{
|
||||
consoleArea = new ConsoleArea;
|
||||
setCentralWidget(consoleArea);
|
||||
|
||||
createActions();
|
||||
// createMenus();
|
||||
}
|
||||
|
||||
void MainWindow::closeEvent(QCloseEvent *event)
|
||||
{
|
||||
event->accept();
|
||||
hbqt_exit( pGT );
|
||||
#if 0
|
||||
if (maybeSave()) {
|
||||
event->accept();
|
||||
} else {
|
||||
event->ignore();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void MainWindow::open()
|
||||
{
|
||||
if (maybeSave()) {
|
||||
QString fileName = QFileDialog::getOpenFileName(this,
|
||||
tr("Open File"), QDir::currentPath());
|
||||
if (!fileName.isEmpty())
|
||||
consoleArea->openImage(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::save()
|
||||
{
|
||||
QAction *action = qobject_cast<QAction *>(sender());
|
||||
QByteArray fileFormat = action->data().toByteArray();
|
||||
saveFile(fileFormat);
|
||||
}
|
||||
|
||||
void MainWindow::penColor()
|
||||
{
|
||||
QColor newColor = QColorDialog::getColor(consoleArea->penColor());
|
||||
if (newColor.isValid())
|
||||
consoleArea->setPenColor(newColor);
|
||||
}
|
||||
|
||||
void MainWindow::penWidth()
|
||||
{
|
||||
bool ok;
|
||||
int newWidth = QInputDialog::getInteger(this, tr("Scribble"),
|
||||
tr("Select pen width:"),
|
||||
consoleArea->penWidth(),
|
||||
1, 50, 1, &ok);
|
||||
if (ok)
|
||||
consoleArea->setPenWidth(newWidth);
|
||||
}
|
||||
|
||||
void MainWindow::about()
|
||||
{
|
||||
QMessageBox::about(this, tr("About Scribble"),
|
||||
tr("<p>The <b>Console</b> example shows how to use QMainWindow as the "
|
||||
"base widget for an application, and how to reimplement some of "
|
||||
"QWidget's event handlers to receive the events generated for "
|
||||
"the application's widgets:</p><p> We reimplement the mouse event "
|
||||
"handlers to facilitate drawing, the paint event handler to "
|
||||
"update the application and the resize event handler to optimize "
|
||||
"the application's appearance. In addition we reimplement the "
|
||||
"close event handler to intercept the close events before "
|
||||
"terminating the application.</p><p> The example also demonstrates "
|
||||
"how to use QPainter to draw an image in real time, as well as "
|
||||
"to repaint widgets.</p>"));
|
||||
}
|
||||
|
||||
void MainWindow::createActions()
|
||||
{
|
||||
openAct = new QAction(tr("&Open..."), this);
|
||||
openAct->setShortcut(tr("Ctrl+O"));
|
||||
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
|
||||
|
||||
foreach (QByteArray format, QImageWriter::supportedImageFormats())
|
||||
{
|
||||
QString text = tr("%1...").arg(QString(format).toUpper());
|
||||
|
||||
QAction *action = new QAction(text, this);
|
||||
action->setData(format);
|
||||
connect(action, SIGNAL(triggered()), this, SLOT(save()));
|
||||
saveAsActs.append(action);
|
||||
}
|
||||
|
||||
printAct = new QAction(tr("&Print..."), this);
|
||||
connect(printAct, SIGNAL(triggered()), consoleArea, SLOT(print()));
|
||||
|
||||
exitAct = new QAction(tr("E&xit"), this);
|
||||
exitAct->setShortcut(tr("Ctrl+Q"));
|
||||
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
|
||||
|
||||
penColorAct = new QAction(tr("&Pen Color..."), this);
|
||||
connect(penColorAct, SIGNAL(triggered()), this, SLOT(penColor()));
|
||||
|
||||
penWidthAct = new QAction(tr("Pen &Width..."), this);
|
||||
connect(penWidthAct, SIGNAL(triggered()), this, SLOT(penWidth()));
|
||||
|
||||
clearScreenAct = new QAction(tr("&Clear Screen"), this);
|
||||
clearScreenAct->setShortcut(tr("Ctrl+L"));
|
||||
connect(clearScreenAct, SIGNAL(triggered()),
|
||||
consoleArea, SLOT(clearImage()));
|
||||
|
||||
aboutAct = new QAction(tr("&About"), this);
|
||||
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
|
||||
|
||||
aboutQtAct = new QAction(tr("About &Qt"), this);
|
||||
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
|
||||
}
|
||||
|
||||
void MainWindow::createMenus()
|
||||
{
|
||||
saveAsMenu = new QMenu(tr("&Save As"), this);
|
||||
foreach (QAction *action, saveAsActs)
|
||||
saveAsMenu->addAction(action);
|
||||
|
||||
fileMenu = new QMenu(tr("&File"), this);
|
||||
fileMenu->addAction(openAct);
|
||||
fileMenu->addMenu(saveAsMenu);
|
||||
fileMenu->addAction(printAct);
|
||||
fileMenu->addSeparator();
|
||||
fileMenu->addAction(exitAct);
|
||||
|
||||
optionMenu = new QMenu(tr("&Options"), this);
|
||||
optionMenu->addAction(penColorAct);
|
||||
optionMenu->addAction(penWidthAct);
|
||||
optionMenu->addSeparator();
|
||||
optionMenu->addAction(clearScreenAct);
|
||||
|
||||
helpMenu = new QMenu(tr("&Help"), this);
|
||||
helpMenu->addAction(aboutAct);
|
||||
helpMenu->addAction(aboutQtAct);
|
||||
|
||||
menuBar()->addMenu(fileMenu);
|
||||
menuBar()->addMenu(optionMenu);
|
||||
menuBar()->addMenu(helpMenu);
|
||||
}
|
||||
|
||||
bool MainWindow::maybeSave()
|
||||
{
|
||||
if (consoleArea->isModified()) {
|
||||
QMessageBox::StandardButton ret;
|
||||
ret = QMessageBox::warning(this, tr("Scribble"),
|
||||
tr("The image has been modified.\n"
|
||||
"Do you want to save your changes?"),
|
||||
QMessageBox::Save | QMessageBox::Discard
|
||||
| QMessageBox::Cancel);
|
||||
if (ret == QMessageBox::Save) {
|
||||
return saveFile("png");
|
||||
} else if (ret == QMessageBox::Cancel) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MainWindow::saveFile(const QByteArray &fileFormat)
|
||||
{
|
||||
QString initialPath = QDir::currentPath() + "/untitled." + fileFormat;
|
||||
|
||||
QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
|
||||
initialPath,
|
||||
tr("%1 Files (*.%2);;All Files (*)")
|
||||
.arg(QString(fileFormat.toUpper()))
|
||||
.arg(QString(fileFormat)));
|
||||
if (fileName.isEmpty()) {
|
||||
return false;
|
||||
} else {
|
||||
return consoleArea->saveImage(fileName, fileFormat);
|
||||
}
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------*/
|
||||
|
||||
|
||||
@@ -56,6 +56,7 @@
|
||||
#define HB_GT_NAME QTC
|
||||
|
||||
#include <Qt/qglobal.h>
|
||||
#include <QtGui/QtGui>
|
||||
#include <QtCore/QObject>
|
||||
#include <QtCore/QList>
|
||||
#include <QtGui/QApplication>
|
||||
@@ -66,6 +67,8 @@
|
||||
#include <QtGui/QPainter>
|
||||
#include <QtGui/QPaintDevice>
|
||||
#include <QtGui/QColor>
|
||||
#include <QtGui/QKeyEvent>
|
||||
#include <QtGui/QMouseEvent>
|
||||
|
||||
#include "hbset.h"
|
||||
#include "hbgtcore.h"
|
||||
@@ -183,7 +186,7 @@ class ConsoleArea : public QWidget
|
||||
public:
|
||||
ConsoleArea(QWidget *parent = 0);
|
||||
|
||||
PHB_GT pGT;
|
||||
PHB_GT pGT;
|
||||
|
||||
bool openImage(const QString &fileName);
|
||||
bool saveImage(const QString &fileName, const char *fileFormat);
|
||||
@@ -194,9 +197,7 @@ public:
|
||||
QColor penColor() const { return myPenColor; }
|
||||
int penWidth() const { return myPenWidth; }
|
||||
|
||||
//Qt::GlobalColor COLORS[ 16 ];
|
||||
QRgb COLORS[ 16 ];
|
||||
//QColor COLORS[ 16 ];
|
||||
void sizeByFont(void);
|
||||
|
||||
public slots:
|
||||
@@ -204,6 +205,8 @@ public slots:
|
||||
void print();
|
||||
|
||||
protected:
|
||||
void keyPressEvent(QKeyEvent *event);
|
||||
void keyReleaseEvent(QKeyEvent *event);
|
||||
void mousePressEvent(QMouseEvent *event);
|
||||
void mouseMoveEvent(QMouseEvent *event);
|
||||
void mouseReleaseEvent(QMouseEvent *event);
|
||||
@@ -238,7 +241,6 @@ public:
|
||||
ConsoleArea *consoleArea;
|
||||
PHB_GT pGT;
|
||||
|
||||
void someFunc(void);
|
||||
protected:
|
||||
void closeEvent(QCloseEvent *event);
|
||||
|
||||
|
||||
@@ -1,288 +0,0 @@
|
||||
/*
|
||||
* $Id$
|
||||
*/
|
||||
|
||||
/*
|
||||
* Harbour Project source code:
|
||||
*
|
||||
* Copyright 2009 Pritpal Bedi <pritpal@vouchcac.com>
|
||||
* www - http://www.harbour-project.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, 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 software; see the file COPYING. If not, write to
|
||||
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
||||
* Boston, MA 02111-1307 USA (or visit the web site http://www.gnu.org/).
|
||||
*
|
||||
* As a special exception, the Harbour Project gives permission for
|
||||
* additional uses of the text contained in its release of Harbour.
|
||||
*
|
||||
* The exception is that, if you link the Harbour libraries with other
|
||||
* files to produce an executable, this does not by itself cause the
|
||||
* resulting executable to be covered by the GNU General Public License.
|
||||
* Your use of that executable is in no way restricted on account of
|
||||
* linking the Harbour library code into it.
|
||||
*
|
||||
* This exception does not however invalidate any other reasons why
|
||||
* the executable file might be covered by the GNU General Public License.
|
||||
*
|
||||
* This exception applies only to the code released by the Harbour
|
||||
* Project under the name Harbour. If you copy code from other
|
||||
* Harbour Project or Free Software Foundation releases into a copy of
|
||||
* Harbour, as the General Public License permits, the exception does
|
||||
* not apply to the code that you add in this way. To avoid misleading
|
||||
* anyone as to the status of such modified files, you must delete
|
||||
* this exception notice from them.
|
||||
*
|
||||
* If you write modifications of your own for Harbour, it is your choice
|
||||
* whether to permit this exception to apply to your modifications.
|
||||
* If you do not wish that, delete this exception notice.
|
||||
*
|
||||
*/
|
||||
/*----------------------------------------------------------------------*/
|
||||
|
||||
#include <QtGui/QtGui>
|
||||
#include <QtGui/QColor>
|
||||
|
||||
#include "gtqtc.h"
|
||||
#include "hbapigt.h"
|
||||
|
||||
class MainWindow;
|
||||
|
||||
ConsoleArea::ConsoleArea(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
{
|
||||
setAttribute(Qt::WA_StaticContents);
|
||||
modified = false;
|
||||
scribbling = false;
|
||||
myPenWidth = 1;
|
||||
myPenColor = Qt::blue;
|
||||
|
||||
COLORS[ 0] = BLACK;
|
||||
COLORS[ 1] = BLUE;
|
||||
COLORS[ 2] = GREEN;
|
||||
COLORS[ 3] = CYAN;
|
||||
COLORS[ 4] = RED;
|
||||
COLORS[ 5] = MAGENTA;
|
||||
COLORS[ 6] = BROWN;
|
||||
COLORS[ 7] = WHITE;
|
||||
COLORS[ 8] = LIGHT_GRAY;
|
||||
COLORS[ 9] = BRIGHT_BLUE;
|
||||
COLORS[10] = BRIGHT_GREEN;
|
||||
COLORS[11] = BRIGHT_CYAN;
|
||||
COLORS[12] = BRIGHT_RED;
|
||||
COLORS[13] = BRIGHT_MAGENTA;
|
||||
COLORS[14] = YELLOW;
|
||||
COLORS[15] = BRIGHT_WHITE;
|
||||
}
|
||||
|
||||
void ConsoleArea::sizeByFont(void)
|
||||
{
|
||||
QPainter painter( this );
|
||||
qFont = QFont( tr("Courier New"), 12, -1, FALSE );
|
||||
qFont = QFont( qFont, painter.device() );
|
||||
QFontMetrics fontMetrics( qFont );
|
||||
fontHeight = fontMetrics.height();
|
||||
fontWidth = fontMetrics.averageCharWidth();
|
||||
resize( fontWidth*80, fontHeight*25 );
|
||||
}
|
||||
|
||||
bool ConsoleArea::openImage(const QString &fileName)
|
||||
{
|
||||
QImage loadedImage;
|
||||
if (!loadedImage.load(fileName))
|
||||
return false;
|
||||
|
||||
QSize newSize = loadedImage.size().expandedTo(size());
|
||||
resizeImage(&loadedImage, newSize);
|
||||
image = loadedImage;
|
||||
modified = false;
|
||||
update();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ConsoleArea::saveImage(const QString &fileName, const char *fileFormat)
|
||||
{
|
||||
QImage visibleImage = image;
|
||||
resizeImage(&visibleImage, size());
|
||||
|
||||
if (visibleImage.save(fileName, fileFormat)) {
|
||||
modified = false;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void ConsoleArea::setPenColor(const QColor &newColor)
|
||||
{
|
||||
myPenColor = newColor;
|
||||
}
|
||||
|
||||
void ConsoleArea::setPenWidth(int newWidth)
|
||||
{
|
||||
myPenWidth = newWidth;
|
||||
}
|
||||
|
||||
void ConsoleArea::clearImage()
|
||||
{
|
||||
image.fill(qRgb(255, 255, 255));
|
||||
modified = true;
|
||||
update();
|
||||
}
|
||||
|
||||
void ConsoleArea::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
if (event->button() == Qt::LeftButton) {
|
||||
lastPoint = event->pos();
|
||||
scribbling = true;
|
||||
}
|
||||
}
|
||||
|
||||
void ConsoleArea::mouseMoveEvent(QMouseEvent *event)
|
||||
{
|
||||
if ((event->buttons() & Qt::LeftButton) && scribbling)
|
||||
drawLineTo(event->pos());
|
||||
}
|
||||
|
||||
void ConsoleArea::mouseReleaseEvent(QMouseEvent *event)
|
||||
{
|
||||
if (event->button() == Qt::LeftButton && scribbling) {
|
||||
drawLineTo(event->pos());
|
||||
scribbling = false;
|
||||
}
|
||||
}
|
||||
|
||||
void ConsoleArea::paintEvent(QPaintEvent * /* event */)
|
||||
{
|
||||
QPainter painter(this);
|
||||
painter.drawImage(QPoint(0, 0), image);
|
||||
{
|
||||
int hgt = height();
|
||||
int fntHeight = (hgt/25);
|
||||
fntHeight = 14;
|
||||
QFont font( tr( "Courier New" ), fntHeight, 10, FALSE );
|
||||
font = QFont(font, painter.device());
|
||||
QFontMetrics fontMetrics( font );
|
||||
int height = fontMetrics.height();
|
||||
painter.setFont(font);
|
||||
painter.setBackgroundMode(Qt::OpaqueMode);
|
||||
{
|
||||
int iRow,iCol,startCol,len ;
|
||||
int iTop;
|
||||
USHORT usChar;
|
||||
BYTE bColor, bAttr, bOldColor = 0;
|
||||
char text[ 4000 ];
|
||||
|
||||
iTop = 0;
|
||||
for( iRow = 0; iRow <= 24; iRow++ )
|
||||
{
|
||||
iCol = startCol = 0;
|
||||
len = 0;
|
||||
iTop = ( iRow * height ) + height;
|
||||
|
||||
while( iCol <= 79 )
|
||||
{
|
||||
if( !HB_GTSELF_GETSCRCHAR( pGT, iRow, iCol, &bColor, &bAttr, &usChar ) )
|
||||
break;
|
||||
|
||||
if( len == 0 )
|
||||
{
|
||||
bOldColor = bColor;
|
||||
}
|
||||
else if( bColor != bOldColor )
|
||||
{
|
||||
QBrush brush( COLORS[ bOldColor >> 4 ] );
|
||||
painter.setBackground( brush );
|
||||
QPen pen( COLORS[ bOldColor ] );
|
||||
painter.setPen( pen );
|
||||
OutputDebugString( "KKKK len=%i bOldColor=%i bColor=%i" );
|
||||
|
||||
painter.drawText( QPointF( startCol,iTop ), QString( text ) );
|
||||
|
||||
bOldColor = bColor;
|
||||
startCol = iCol;
|
||||
len = 0;
|
||||
}
|
||||
text[ len++ ] = ( char ) usChar;
|
||||
iCol++;
|
||||
}
|
||||
if( len > 0 )
|
||||
{
|
||||
QBrush brush( COLORS[ bOldColor >> 4 ] );
|
||||
painter.setBackground( brush );
|
||||
QPen pen( COLORS[ bOldColor ] );
|
||||
painter.setPen( pen );
|
||||
|
||||
painter.drawText(QPointF(startCol,iTop),QString(text));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ConsoleArea::resizeEvent(QResizeEvent *event)
|
||||
{
|
||||
if (width() > image.width() || height() > image.height()) {
|
||||
int newWidth = qMax(width() + 128, image.width());
|
||||
int newHeight = qMax(height() + 128, image.height());
|
||||
resizeImage(&image, QSize(newWidth, newHeight));
|
||||
update();
|
||||
}
|
||||
QWidget::resizeEvent(event);
|
||||
}
|
||||
|
||||
void ConsoleArea::drawLineTo(const QPoint &endPoint)
|
||||
{
|
||||
QPainter painter(&image);
|
||||
painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap,
|
||||
Qt::RoundJoin));
|
||||
painter.drawLine(lastPoint, endPoint);
|
||||
modified = true;
|
||||
|
||||
int rad = (myPenWidth / 2) + 2;
|
||||
update(QRect(lastPoint, endPoint).normalized().adjusted(-rad, -rad, +rad, +rad));
|
||||
lastPoint = endPoint;
|
||||
}
|
||||
|
||||
void ConsoleArea::resizeImage(QImage *image, const QSize &newSize)
|
||||
{
|
||||
if (image->size() == newSize)
|
||||
return;
|
||||
|
||||
QImage newImage(newSize, QImage::Format_RGB32);
|
||||
newImage.fill(qRgb(255, 255, 255));
|
||||
QPainter painter(&newImage);
|
||||
painter.drawImage(QPoint(0, 0), *image);
|
||||
*image = newImage;
|
||||
}
|
||||
|
||||
void ConsoleArea::print()
|
||||
{
|
||||
#ifndef QT_NO_PRINTER
|
||||
QPrinter printer(QPrinter::HighResolution);
|
||||
|
||||
QPrintDialog *printDialog = new QPrintDialog(&printer, this);
|
||||
if (printDialog->exec() == QDialog::Accepted) {
|
||||
QPainter painter(&printer);
|
||||
QRect rect = painter.viewport();
|
||||
QSize size = image.size();
|
||||
size.scale(rect.size(), Qt::KeepAspectRatio);
|
||||
painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
|
||||
painter.setWindow(image.rect());
|
||||
painter.drawImage(0, 0, image);
|
||||
}
|
||||
#endif // QT_NO_PRINTER
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------*/
|
||||
|
||||
@@ -1,236 +0,0 @@
|
||||
/*
|
||||
* $Id$
|
||||
*/
|
||||
|
||||
/*
|
||||
* Harbour Project source code:
|
||||
*
|
||||
* Copyright 2009 Pritpal Bedi <pritpal@vouchcac.com>
|
||||
* www - http://www.harbour-project.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, 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 software; see the file COPYING. If not, write to
|
||||
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
||||
* Boston, MA 02111-1307 USA (or visit the web site http://www.gnu.org/).
|
||||
*
|
||||
* As a special exception, the Harbour Project gives permission for
|
||||
* additional uses of the text contained in its release of Harbour.
|
||||
*
|
||||
* The exception is that, if you link the Harbour libraries with other
|
||||
* files to produce an executable, this does not by itself cause the
|
||||
* resulting executable to be covered by the GNU General Public License.
|
||||
* Your use of that executable is in no way restricted on account of
|
||||
* linking the Harbour library code into it.
|
||||
*
|
||||
* This exception does not however invalidate any other reasons why
|
||||
* the executable file might be covered by the GNU General Public License.
|
||||
*
|
||||
* This exception applies only to the code released by the Harbour
|
||||
* Project under the name Harbour. If you copy code from other
|
||||
* Harbour Project or Free Software Foundation releases into a copy of
|
||||
* Harbour, as the General Public License permits, the exception does
|
||||
* not apply to the code that you add in this way. To avoid misleading
|
||||
* anyone as to the status of such modified files, you must delete
|
||||
* this exception notice from them.
|
||||
*
|
||||
* If you write modifications of your own for Harbour, it is your choice
|
||||
* whether to permit this exception to apply to your modifications.
|
||||
* If you do not wish that, delete this exception notice.
|
||||
*
|
||||
*/
|
||||
/*----------------------------------------------------------------------*/
|
||||
|
||||
#include <QtGui/QtGui>
|
||||
|
||||
#include "gtqtc.h"
|
||||
|
||||
MainWindow::MainWindow()
|
||||
{
|
||||
consoleArea = new ConsoleArea;
|
||||
setCentralWidget(consoleArea);
|
||||
|
||||
createActions();
|
||||
// createMenus();
|
||||
}
|
||||
|
||||
void MainWindow::someFunc(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void MainWindow::closeEvent(QCloseEvent *event)
|
||||
{
|
||||
event->accept();
|
||||
hbqt_exit( pGT );
|
||||
#if 0
|
||||
if (maybeSave()) {
|
||||
event->accept();
|
||||
} else {
|
||||
event->ignore();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void MainWindow::open()
|
||||
{
|
||||
if (maybeSave()) {
|
||||
QString fileName = QFileDialog::getOpenFileName(this,
|
||||
tr("Open File"), QDir::currentPath());
|
||||
if (!fileName.isEmpty())
|
||||
consoleArea->openImage(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::save()
|
||||
{
|
||||
QAction *action = qobject_cast<QAction *>(sender());
|
||||
QByteArray fileFormat = action->data().toByteArray();
|
||||
saveFile(fileFormat);
|
||||
}
|
||||
|
||||
void MainWindow::penColor()
|
||||
{
|
||||
QColor newColor = QColorDialog::getColor(consoleArea->penColor());
|
||||
if (newColor.isValid())
|
||||
consoleArea->setPenColor(newColor);
|
||||
}
|
||||
|
||||
void MainWindow::penWidth()
|
||||
{
|
||||
bool ok;
|
||||
int newWidth = QInputDialog::getInteger(this, tr("Scribble"),
|
||||
tr("Select pen width:"),
|
||||
consoleArea->penWidth(),
|
||||
1, 50, 1, &ok);
|
||||
if (ok)
|
||||
consoleArea->setPenWidth(newWidth);
|
||||
}
|
||||
|
||||
void MainWindow::about()
|
||||
{
|
||||
QMessageBox::about(this, tr("About Scribble"),
|
||||
tr("<p>The <b>Console</b> example shows how to use QMainWindow as the "
|
||||
"base widget for an application, and how to reimplement some of "
|
||||
"QWidget's event handlers to receive the events generated for "
|
||||
"the application's widgets:</p><p> We reimplement the mouse event "
|
||||
"handlers to facilitate drawing, the paint event handler to "
|
||||
"update the application and the resize event handler to optimize "
|
||||
"the application's appearance. In addition we reimplement the "
|
||||
"close event handler to intercept the close events before "
|
||||
"terminating the application.</p><p> The example also demonstrates "
|
||||
"how to use QPainter to draw an image in real time, as well as "
|
||||
"to repaint widgets.</p>"));
|
||||
}
|
||||
|
||||
void MainWindow::createActions()
|
||||
{
|
||||
openAct = new QAction(tr("&Open..."), this);
|
||||
openAct->setShortcut(tr("Ctrl+O"));
|
||||
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
|
||||
|
||||
foreach (QByteArray format, QImageWriter::supportedImageFormats())
|
||||
{
|
||||
QString text = tr("%1...").arg(QString(format).toUpper());
|
||||
|
||||
QAction *action = new QAction(text, this);
|
||||
action->setData(format);
|
||||
connect(action, SIGNAL(triggered()), this, SLOT(save()));
|
||||
saveAsActs.append(action);
|
||||
}
|
||||
|
||||
printAct = new QAction(tr("&Print..."), this);
|
||||
connect(printAct, SIGNAL(triggered()), consoleArea, SLOT(print()));
|
||||
|
||||
exitAct = new QAction(tr("E&xit"), this);
|
||||
exitAct->setShortcut(tr("Ctrl+Q"));
|
||||
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
|
||||
|
||||
penColorAct = new QAction(tr("&Pen Color..."), this);
|
||||
connect(penColorAct, SIGNAL(triggered()), this, SLOT(penColor()));
|
||||
|
||||
penWidthAct = new QAction(tr("Pen &Width..."), this);
|
||||
connect(penWidthAct, SIGNAL(triggered()), this, SLOT(penWidth()));
|
||||
|
||||
clearScreenAct = new QAction(tr("&Clear Screen"), this);
|
||||
clearScreenAct->setShortcut(tr("Ctrl+L"));
|
||||
connect(clearScreenAct, SIGNAL(triggered()),
|
||||
consoleArea, SLOT(clearImage()));
|
||||
|
||||
aboutAct = new QAction(tr("&About"), this);
|
||||
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
|
||||
|
||||
aboutQtAct = new QAction(tr("About &Qt"), this);
|
||||
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
|
||||
}
|
||||
|
||||
void MainWindow::createMenus()
|
||||
{
|
||||
saveAsMenu = new QMenu(tr("&Save As"), this);
|
||||
foreach (QAction *action, saveAsActs)
|
||||
saveAsMenu->addAction(action);
|
||||
|
||||
fileMenu = new QMenu(tr("&File"), this);
|
||||
fileMenu->addAction(openAct);
|
||||
fileMenu->addMenu(saveAsMenu);
|
||||
fileMenu->addAction(printAct);
|
||||
fileMenu->addSeparator();
|
||||
fileMenu->addAction(exitAct);
|
||||
|
||||
optionMenu = new QMenu(tr("&Options"), this);
|
||||
optionMenu->addAction(penColorAct);
|
||||
optionMenu->addAction(penWidthAct);
|
||||
optionMenu->addSeparator();
|
||||
optionMenu->addAction(clearScreenAct);
|
||||
|
||||
helpMenu = new QMenu(tr("&Help"), this);
|
||||
helpMenu->addAction(aboutAct);
|
||||
helpMenu->addAction(aboutQtAct);
|
||||
|
||||
menuBar()->addMenu(fileMenu);
|
||||
menuBar()->addMenu(optionMenu);
|
||||
menuBar()->addMenu(helpMenu);
|
||||
}
|
||||
|
||||
bool MainWindow::maybeSave()
|
||||
{
|
||||
if (consoleArea->isModified()) {
|
||||
QMessageBox::StandardButton ret;
|
||||
ret = QMessageBox::warning(this, tr("Scribble"),
|
||||
tr("The image has been modified.\n"
|
||||
"Do you want to save your changes?"),
|
||||
QMessageBox::Save | QMessageBox::Discard
|
||||
| QMessageBox::Cancel);
|
||||
if (ret == QMessageBox::Save) {
|
||||
return saveFile("png");
|
||||
} else if (ret == QMessageBox::Cancel) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MainWindow::saveFile(const QByteArray &fileFormat)
|
||||
{
|
||||
QString initialPath = QDir::currentPath() + "/untitled." + fileFormat;
|
||||
|
||||
QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
|
||||
initialPath,
|
||||
tr("%1 Files (*.%2);;All Files (*)")
|
||||
.arg(QString(fileFormat.toUpper()))
|
||||
.arg(QString(fileFormat)));
|
||||
if (fileName.isEmpty()) {
|
||||
return false;
|
||||
} else {
|
||||
return consoleArea->saveImage(fileName, fileFormat);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user