diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..8050f27 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,36 @@ +/* qlscribe - Qt based application to print lightScribe discs + + Copyright (C) 2009 Vyacheslav Kononenko + + 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 + + $Id:$ */ + +#include +#include "mainwindow.h" + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + MainWindow mwindow; + mwindow.show(); + //MyObject obj; + //MyDialog dialog; + + //dialog.connect(dialog.aButton, SIGNAL(clicked()), SLOT(close())); + //dialog.show(); + + return app.exec(); +} diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp new file mode 100644 index 0000000..d6c513f --- /dev/null +++ b/src/mainwindow.cpp @@ -0,0 +1,315 @@ +/* qlscribe - Qt based application to print lightScribe discs + + Copyright (C) 2009 Vyacheslav Kononenko + + 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 + + $Id:$ */ + +#include "mainwindow.h" +#include "qgraphicsroundtextitem.h" +#include "qcdview.h" +#include "qcdscene.h" +#include "qshapefactory.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +/*#include +#include +#include +#include +#include */ + +//#include + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + m_mdiArea( new QMdiArea( this ) ), + m_menuFile( 0 ), m_menuInsert( 0 ), + m_insertMapper( new QSignalMapper( this ) ) +{ + setCentralWidget( m_mdiArea ); + + m_menuFile = menuBar()->addMenu( tr( "File", "Menu item \"File\"" ) ); + + m_menuFile->addAction( tr( "New", "Menu item \"New\"" ), + this, + SLOT(onMenuNew()) ); + + m_menuFile->addAction( tr( "Open...", "Menu item \"Open\"" ), + this, + SLOT(onMenuOpen()) ); + + m_menuFile->addAction( tr( "Save as...", "Menu item \"Save as\"" ), + this, + SLOT(onMenuSaveAs()) ); + + m_menuInsert = menuBar()->addMenu( tr( "Insert", "Menu item \"Insert\"" ) ); + + + //m_ui->graphicsView->setScene( m_scene ); + //m_ui->graphicsView->fitInView( m_scene->sceneRect(), Qt::KeepAspectRatioByExpanding ); + //m_ui->graphicsView->centerOn( 0, 0 ); + //m_ui->graphicsView->scale( 10, 10 ); + //m_ui->graphicsView->setInteractive( true ); + //QFont font = QFontDialog::getFont( 0 ); + //font.setPointSizeF( font.pointSizeF() / 2.0 ) ; + //m_scene->addSimpleText( "Test and test and test again", font ); + + QShapeFactory &sfactory = QShapeFactory::instance(); + + for( QShapeFactory::iterator i = sfactory.begin(); i != sfactory.end(); ++i ) { + QAction *action = new QAction( i->second->menuName(), this ); + m_menuInsert->addAction( action ); + m_insertMapper->setMapping( action, i->first ); + connect( action, SIGNAL(triggered()), m_insertMapper, SLOT(map()) ); + } + connect( m_insertMapper, SIGNAL(mapped(int)), this, SLOT(onMenuInsert(int)) ); +} + +MainWindow::~MainWindow() +{ +} + +void MainWindow::onMenuNew() +{ + QCDView *newView = new QCDView; + newView->setScene( new QCDScene( newView ) ); + //newView->setWindowTitle( "Baba" ); + QMdiSubWindow *subWindow = m_mdiArea->addSubWindow( newView ); + subWindow->show(); +} + +inline +QCDScene *getScene( QMdiArea *area ) +{ + QMdiSubWindow *subWindow = area->activeSubWindow(); + if( !subWindow ) + return 0; + + QCDView *cdview = dynamic_cast< QCDView * >( subWindow->widget() ); + if( !cdview ) + return 0; + + return cdview->scene(); +} + +void MainWindow::onMenuInsert( int id ) +{ + QCDScene *cdscene = getScene( m_mdiArea ); + if( !cdscene ) + return; + + QShapeFactory &sfactory = QShapeFactory::instance(); + QGraphicsItem *item = sfactory.create( id ); + if( !item ) { + QMessageBox::warning( this, + "Warning", + tr( "Creator for type id %n returned zero pointer", 0, id ) ); + return; + } + if( !sfactory.edit( item, this ) ) { + delete item; + return; + } + + item->setFlag( QGraphicsItem::ItemIsMovable, true ); + item->setFlag( QGraphicsItem::ItemIsSelectable, true ); + cdscene->addItem( item ); +} + +void MainWindow::onMenuOpen() +{ + QString fileName = QFileDialog::getOpenFileName( this, + tr( "Open:" ), + QString(), + tr("qlscribe document (*.qlx)") ); + + if( fileName.isNull() ) + return; + + QFile file( fileName ); + if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) { + QMessageBox::critical( this, tr( "Error" ), tr( "Cannot open file for reading\n" ) + fileName ); + return; + } + + QCDView *newView = new QCDView; + QCDScene *scene = new QCDScene( newView ); + newView->setScene( scene ); + + QXmlStreamReader reader( &file ); + try { + scene->read( reader ); + } + catch( const QString &err ) { + QMessageBox::critical( this, tr( "Error" ), tr( "Cannot read file " ) + fileName + "\n" + err ); + delete newView; + return; + } + + //newView->setWindowTitle( "Baba" ); + QMdiSubWindow *subWindow = m_mdiArea->addSubWindow( newView ); + subWindow->show(); +} + +void MainWindow::onMenuSaveAs() +{ + QCDScene *cdscene = getScene( m_mdiArea ); + if( !cdscene ) + return; + + QString fileName = + QFileDialog::getSaveFileName( this, + tr( "Save as:" ), + QString(), + tr("qlscribe document (*.qlx)") ); + if( fileName.isNull() ) + return; + + if( !fileName.contains( '.' ) ) + fileName += ".qlx"; + + QFile file( fileName ); + if( !file.open( QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text ) ) { + QMessageBox::warning( this, tr( "Warning" ), tr( "Cannot open file for writing: " ) + fileName ); + return; + } + QXmlStreamWriter writer( &file ); + writer.setAutoFormatting( true ); + cdscene->write( writer ); +} + +/*void MainWindow::onClicked2() +{ + QGraphicsRoundTextItem *titem = new QGraphicsRoundTextItem; + titem->setText( "Hello world!" ); + titem->setRadius( 50 ); + titem->setFont( QFont("Helvetica", 8 ) ); + static int a = 0; + titem->setAlignment( Qt::AlignCenter ); + titem->setOutside( false ); + titem->setAngle( a ); + a += 90; + + //m_scene->addItem( titem ); +}*/ + +/*void MainWindow::onClicked3() +{ + QGraphicsRoundTextItem *titem = new QGraphicsRoundTextItem; + titem->setText( "Hello world!" ); + titem->setRadius( 50 ); + titem->setFont( QFont("Helvetica", 2 ) ); + static int a = 0; + titem->setAlignment( Qt::AlignCenter ); + titem->setOutside( true ); + titem->setAngle( a ); + a += 90; + + //m_scene->addItem( titem ); +}*/ + +/*void checkLS( LSError err, const QString &name ) +{ + if( err == LS_OK ) + return; + + QMessageBox::warning( 0, "Failed", name + QString( " failed: 0x" ) + QString::number( err,16 ) ); + throw 1; +} + +QString color( const LS_ColorRGB &col ) +{ + return QString::number( col.red, 16 ) + QString::number( col.green, 16 ) + QString::number( col.blue, 16 ); + }*/ + +/* +const size_t bitmapHeaderSize = 54; + +void MainWindow::onClicked() +{ + QImage image( 2772, 2772, QImage::Format_RGB888 ); + //QImage image( 800, 800, QImage::Format_RGB888 ); + image.fill( 0xFFFFFFFF ); + + QPainter painter( &image ); + m_scene->render( &painter, image.rect() ); + + image.setDotsPerMeterX( 23622 ); + image.setDotsPerMeterY( 23622 ); + + QByteArray ba; + QBuffer buffer( &ba ); + buffer.open( QIODevice::WriteOnly ); + image.save( &buffer, "bmp", 100 ); + + { + std::ofstream of( "test1.bmp" ); + of.write( ba.constData(), ba.size() ); + } + //return; + + //QMessageBox::information( this, "Buf size", QString::number( ba.size() ) ); + + + try { + LS_DiscPrintMgrHandle mgrHandle; + checkLS( LS_DiscPrintMgr_Create( &mgrHandle ), "LS_DiscPrintMgr_Create" ); + + LS_EnumDiscPrintersHandle enumHandle; + checkLS( LS_DiscPrintMgr_EnumDiscPrinters( mgrHandle, &enumHandle ), "LS_DiscPrintMgr_EnumDiscPrinters" ); + + //unsigned long count = 0; + //checkLS( LS_EnumDiscPrinters_Count( enumHandle, &count ), "LS_EnumDiscPrinters_Count" ); + //QMessageBox::information( this, "Count", QString::number( count ) ); + LS_DiscPrinterHandle printerHandle; + checkLS( LS_EnumDiscPrinters_Item( enumHandle, 0, &printerHandle ), "LS_EnumDiscPrinters_Item" ); + + LS_DiscPrintSessionHandle sessionHandle; + checkLS( LS_DiscPrinter_OpenPrintSession( printerHandle, &sessionHandle ), "LS_DiscPrinter_OpenPrintSession" ); + + LS_Size size; + size.x = 1000; + size.y = 1000; + checkLS( LS_DiscPrintSession_PrintPreview( sessionHandle, + LS_windows_bitmap, + LS_label_mode_full, + LS_draw_default, + LS_quality_normal, + LS_media_recognized, + ba.data() + 14, bitmapHeaderSize - 14, + ba.data() + bitmapHeaderSize, ba.size() - bitmapHeaderSize, + "test.bmp", LS_windows_bitmap, size, false ), + "LS_DiscPrintSession_PrintPreview" ); + + checkLS( LS_DiscPrintSession_Destroy( &sessionHandle ), "LS_DiscPrintSession_Destroy" ); + + checkLS( LS_DiscPrinter_Destroy( &printerHandle ), "LS_DiscPrinter_Destroy" ); + checkLS( LS_EnumDiscPrinters_Destroy( &enumHandle ), "LS_EnumDiscPrinters_Destroy" ); + checkLS( LS_DiscPrintMgr_Destroy( &mgrHandle ), "LS_DiscPrintMgr_Destroy" ); + } + catch( int ) { + } + +} +*/ diff --git a/src/mainwindow.h b/src/mainwindow.h new file mode 100644 index 0000000..f2efd2d --- /dev/null +++ b/src/mainwindow.h @@ -0,0 +1,51 @@ +/* qlscribe - Qt based application to print lightScribe discs + + Copyright (C) 2009 Vyacheslav Kononenko + + 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 + + $Id:$ */ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +class QMdiArea; +class QSignalMapper; + +class MainWindow : public QMainWindow { + Q_OBJECT + Q_DISABLE_COPY(MainWindow) +public: + explicit MainWindow(QWidget *parent = 0); + virtual ~MainWindow(); + +private slots: + void onMenuNew(); + void onMenuOpen(); + void onMenuSaveAs(); + void onMenuInsert( int id ); + +private: + QMdiArea *m_mdiArea; + + QMenu *m_menuFile; + QMenu *m_menuInsert; + + QSignalMapper *m_insertMapper; +}; + +#endif // MAINWINDOW_H diff --git a/src/qcdscene.cpp b/src/qcdscene.cpp new file mode 100644 index 0000000..9122829 --- /dev/null +++ b/src/qcdscene.cpp @@ -0,0 +1,123 @@ +/* qlscribe - Qt based application to print lightScribe discs + + Copyright (C) 2009 Vyacheslav Kononenko + + 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 + + $Id:$ */ + +#include "qcdscene.h" +#include "qshapefactory.h" + +#include +#include +#include +#include +#include + +QCDScene::QCDScene( QObject * parent ) + : QGraphicsScene( parent ) +{ + setSceneRect( -60.0, -60.0, 60.0 * 2, 60.0 * 2 ); +} + +//#include + +QCDScene::~QCDScene() +{ +} + +void QCDScene::write( QXmlStreamWriter &writer ) +{ + writer.writeStartDocument(); + writer.writeStartElement( "scene" ); + + QShapeFactory &sfactory = QShapeFactory::instance(); + QList list = items(); + for( QList::const_iterator it = list.begin(); it != list.end(); ++it ) { + QShapeFactory::iterator f = sfactory.find( (*it)->type() ); + if( f == sfactory.end() ) { + QMessageBox::warning( 0, tr( "Warning" ), tr( "Cannot find controller for type %n", 0, (*it)->type() ) ); + continue; + } + f->second->write( writer, *it ); + } + + writer.writeEndElement(); + writer.writeEndDocument(); +} + +void QCDScene::read( QXmlStreamReader &reader ) +{ + bool gotScene = false; + + QShapeFactory &sfactory = QShapeFactory::instance(); + + while( !reader.atEnd() ) { + QXmlStreamReader::TokenType ttype = reader.readNext(); + if( ttype != QXmlStreamReader::StartElement ) + continue; + + QString elementName = reader.name().toString(); + if( !gotScene ) { + if( elementName != "scene" ) + throw QString( "QCDScene: missing expected root element \"scene\", got \"" ) + + elementName + "\" intead"; + + gotScene = true; + continue; + } + + if( elementName == "item" ) { + QString type = reader.attributes().value( "type" ).toString(); + QShapeFactory::iterator f = sfactory.find( type ); + if( f == sfactory.end() ) { + QMessageBox::warning( 0, + "Warning", + QString( "QCDScene: unknown item type \"" ) + + type + "\"" ); + continue; + } + QGraphicsItem *item = f->second->read( reader ); + if( item ) { + item->setFlag( QGraphicsItem::ItemIsMovable, true ); + item->setFlag( QGraphicsItem::ItemIsSelectable, true ); + addItem( item ); + } + } else + throw QString( "QCDScene: unknown element \"" ) + elementName + "\""; + } + +} + +void QCDScene::contextMenuEvent( QGraphicsSceneContextMenuEvent *mouseEvent ) +{ + QGraphicsItem *item = itemAt( mouseEvent->scenePos() ); + if( !item ) + return; + item->setSelected( true ); + QMenu menu; + menu.addAction( tr( "edit..." ), this, SLOT( onMenuEdit() ) ); + menu.exec( mouseEvent->screenPos() ); +} + +void QCDScene::onMenuEdit() +{ + QList list = selectedItems(); + if( list.empty() ) + return; + QGraphicsItem *item = list.front(); + QShapeFactory::instance().edit( item, 0 ); +} diff --git a/src/qcdscene.h b/src/qcdscene.h new file mode 100644 index 0000000..bf4de51 --- /dev/null +++ b/src/qcdscene.h @@ -0,0 +1,45 @@ +/* qlscribe - Qt based application to print lightScribe discs + + Copyright (C) 2009 Vyacheslav Kononenko + + 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 + + $Id:$ */ + +#ifndef QCDSCENE_H +#define QCDSCENE_H + +#include + +class QXmlStreamWriter; +class QXmlStreamReader; + +class QCDScene : public QGraphicsScene { + Q_OBJECT +public: + QCDScene( QObject * parent = 0 ); + virtual ~QCDScene(); + + void write( QXmlStreamWriter &writer ); + void read( QXmlStreamReader &reader ); + +protected: + virtual void contextMenuEvent( QGraphicsSceneContextMenuEvent *contextMenuEvent ); + +private slots: + void onMenuEdit(); +}; + +#endif //QCDSCENE_H diff --git a/src/qcdview.cpp b/src/qcdview.cpp new file mode 100644 index 0000000..da10cc6 --- /dev/null +++ b/src/qcdview.cpp @@ -0,0 +1,109 @@ +/* qlscribe - Qt based application to print lightScribe discs + + Copyright (C) 2009 Vyacheslav Kononenko + + 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 + + $Id:$ */ + +#include "qcdview.h" +#include "qcdscene.h" + +QCDView::QCDView( QWidget *parent ) + : QGraphicsView( parent ), + m_mask( new QPixmap ) +{ + setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); + setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); + centerOn( 0, 0 ); +} + +QCDView::~QCDView() +{ +} + +QSize QCDView::sizeHint () const +{ + return QSize( 600, 600 ); +} + +void QCDView::setScene( QCDScene *scene ) +{ + QGraphicsView::setScene( scene ); +} + +QCDScene *QCDView::scene() const +{ + return static_cast< QCDScene * >( QGraphicsView::scene() ); +} + +inline +void drawCircle( QPainter *painter, double radi ) +{ + painter->drawEllipse( -radi, -radi, radi * 2, radi * 2 ); +} + +void QCDView::drawCD( QPainter *painter, const QRectF & rect, bool alpha ) +{ + painter->setBrush( alpha ? Qt::lightGray : Qt::gray ); + painter->drawRect( rect ); + + painter->setBrush( alpha ? Qt::black : Qt::white ); + drawCircle( painter, 59.0 ); + + painter->setBrush( alpha ? Qt::lightGray : Qt::darkGray ); + drawCircle( painter, 21.7 ); + + painter->setBrush( alpha ? Qt::white : Qt::gray ); + drawCircle( painter, 10.7 ); +} + +void QCDView::drawForeground ( QPainter * painter, const QRectF & rect ) +{ + if( !painter ) return; + if( m_mask->size() != frameSize() ) { + *m_mask = QPixmap( frameSize() ); + + { + QPainter paint( m_mask ); + paint.setWorldTransform( painter->transform() ); + drawCD( &paint, rect, false ); + } + + { + QPixmap alpha( frameSize() ); + QPainter paint( &alpha ); + paint.setWorldTransform( painter->transform() ); + drawCD( &paint, rect, true ); + m_mask->setAlphaChannel( alpha ); + } + + } + painter->resetTransform(); + painter->drawPixmap( 0, 0, *m_mask ); +} + +void QCDView::drawBackground ( QPainter * painter, const QRectF & rect ) +{ + if( painter ) + drawCD( painter, rect, false ); +} + +void QCDView::resizeEvent( QResizeEvent * event ) +{ + QGraphicsScene *scn = scene(); + if( scn ) + fitInView( scn->sceneRect(), Qt::KeepAspectRatio ); +} diff --git a/src/qcdview.h b/src/qcdview.h new file mode 100644 index 0000000..68aba33 --- /dev/null +++ b/src/qcdview.h @@ -0,0 +1,53 @@ +/* qlscribe - Qt based application to print lightScribe discs + + Copyright (C) 2009 Vyacheslav Kononenko + + 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 + + $Id:$ */ + +#ifndef QCDVIEW_H +#define QCDVIEW_H + +#include + +class QPixmap; +class QCDScene; + +class QCDView : public QGraphicsView { + //Q_OBJECT +public: + QCDView( QWidget *parent = 0 ); + virtual ~QCDView(); + //QCDView( QGraphicsScene * scene, QWidget * parent = 0 ); + + void setScene( QCDScene *scene ); + QCDScene *scene() const; + + virtual QSize sizeHint () const; + +protected: + + void drawForeground ( QPainter * painter, const QRectF & rect ); + void drawBackground ( QPainter * painter, const QRectF & rect ); + void resizeEvent ( QResizeEvent * event ); + +private: + void drawCD( QPainter * painter, const QRectF & rect, bool alpha ); + + QPixmap *m_mask; +}; + +#endif // QCDVIEW_H diff --git a/src/qdialogroundtext.cpp b/src/qdialogroundtext.cpp new file mode 100644 index 0000000..8d078e9 --- /dev/null +++ b/src/qdialogroundtext.cpp @@ -0,0 +1,187 @@ +/* qlscribe - Qt based application to print lightScribe discs + + Copyright (C) 2009 Vyacheslav Kononenko + + 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 + + $Id:$ */ + +#include "qdialogroundtext.h" +#include "ui_qdialogroundtext.h" +#include "qgraphicsroundtextitem.h" +#include "qcdscene.h" + +#include +#include +#include + +QDialogRoundText::QDialogRoundText(QWidget *parent) : + QItemDialog( parent ), + m_ui( new Ui::QDialogRoundText ), + m_item( 0 ) +{ + m_ui->setupUi(this); + connect( m_ui->btnFont, SIGNAL(clicked()), this, SLOT(onFont()) ); + connect( m_ui->btnColor, SIGNAL(clicked()), this, SLOT(onColor()) ); +} + +QDialogRoundText::~QDialogRoundText() +{ + delete m_ui; +} + +void QDialogRoundText::changeEvent(QEvent *e) +{ + switch (e->type()) { + case QEvent::LanguageChange: + m_ui->retranslateUi(this); + break; + default: + break; + } +} + +bool QDialogRoundText::exec( QGraphicsItem *graphicsItem ) +{ + QGraphicsRoundTextItem *item = dynamic_cast< QGraphicsRoundTextItem * >( graphicsItem ); + if( !item ) + return false; + + QCDScene scene; + m_ui->cdView->setScene( &scene ); + m_item = new QGraphicsRoundTextItem; + + m_item->setPos( item->pos() ); + m_item->setText( item->text() ); + m_item->setFont( item->font() ); + m_item->setBrush( item->brush() ); + m_item->setRadius( item->radius() ); + m_item->setAlignment( item->alignment() ); + m_item->setOutside( item->outside() ); + + scene.addItem( m_item ); + + m_ui->spinX->setValue( m_item->pos().x() ); + m_ui->spinY->setValue( m_item->pos().y() ); + m_ui->spinRadi->setValue( m_item->radius() ); + m_ui->spinAngle->setValue( m_item->angle() ); + + int index = 0; + if( m_item->alignment() == Qt::AlignRight ) index = 2; + else if( m_item->alignment() == Qt::AlignCenter ) index = 1; + + m_ui->comboAlignment->setCurrentIndex( index ); + m_ui->comboLocation->setCurrentIndex( m_item->outside() ); + m_ui->textEdit->setText( m_item->text() ); + + fontChanged(); + colorChanged(); + + connect( m_ui->textEdit, SIGNAL(textEdited(const QString&)), + this, SLOT(textChanged()) ); + + connect( m_ui->spinX, SIGNAL(valueChanged(double)), + this, SLOT(posChanged()) ); + + connect( m_ui->spinY, SIGNAL(valueChanged(double)), + this, SLOT(posChanged()) ); + + connect( m_ui->spinRadi, SIGNAL(valueChanged(double)), + this, SLOT(radiChanged()) ); + + connect( m_ui->spinAngle, SIGNAL(valueChanged(double)), + this, SLOT(angleChanged()) ); + + connect( m_ui->comboAlignment, SIGNAL(currentIndexChanged(int)), + this, SLOT(alignChanged()) ); + + connect( m_ui->comboLocation, SIGNAL(currentIndexChanged(int)), + this, SLOT(locChanged()) ); + + if( QDialog::exec() == Rejected ) return false; + + item->setPos( m_item->pos() ); + item->setText( m_item->text() ); + item->setFont( m_item->font() ); + item->setBrush( m_item->brush() ); + item->setRadius( m_item->radius() ); + item->setAlignment( m_item->alignment() ); + item->setOutside( m_item->outside() ); + + return true; +} + +void QDialogRoundText::onFont() +{ + bool ok = true; + QFont font = QFontDialog::getFont( &ok, m_item->font(), this ); + if( ok ) { + m_item->setFont( font ); + fontChanged(); + } +} + +void QDialogRoundText::onColor() +{ + QColor color = QColorDialog::getColor( m_item->brush().color(), this ); + if( color.isValid() ){ + m_item->setBrush( color ); + colorChanged(); + } +} + +void QDialogRoundText::fontChanged() +{ + m_ui->fontName->setCurrentFont( m_item->font() ); +} + +void QDialogRoundText::colorChanged() +{ + QPixmap pixmap( m_ui->colorWidget->size() ); + pixmap.fill( m_item->brush().color() ); + m_ui->colorWidget->setPixmap( pixmap ); +} + + +void QDialogRoundText::textChanged() +{ + m_item->setText( m_ui->textEdit->text() ); +} + +void QDialogRoundText::posChanged() +{ + m_item->setPos( m_ui->spinX->value(), m_ui->spinY->value() ); +} + +void QDialogRoundText::radiChanged() +{ + m_item->setRadius( m_ui->spinRadi->value() ); +} + +void QDialogRoundText::angleChanged() +{ + m_item->setAngle( m_ui->spinAngle->value() ); +} + +void QDialogRoundText::alignChanged() +{ + Qt::Alignment align[] = { Qt::AlignLeft, Qt::AlignCenter, Qt::AlignRight }; + m_item->setAlignment( align[ m_ui->comboAlignment->currentIndex() ] ); +} + +void QDialogRoundText::locChanged() +{ + m_item->setOutside( m_ui->comboLocation->currentIndex() ); +} diff --git a/src/qdialogroundtext.h b/src/qdialogroundtext.h new file mode 100644 index 0000000..2795176 --- /dev/null +++ b/src/qdialogroundtext.h @@ -0,0 +1,61 @@ +/* qlscribe - Qt based application to print lightScribe discs + + Copyright (C) 2009 Vyacheslav Kononenko + + 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 + + $Id:$ */ + +#ifndef QDIALOGROUNDTEXT_H +#define QDIALOGROUNDTEXT_H + +#include "qshapefactory.h" + +namespace Ui { + class QDialogRoundText; +} + +class QGraphicsRoundTextItem; + +class QDialogRoundText : public QItemDialog { + Q_OBJECT + Q_DISABLE_COPY(QDialogRoundText) +public: + explicit QDialogRoundText( QWidget *parent ); + virtual ~QDialogRoundText(); + + virtual bool exec( QGraphicsItem *item ); + +protected: + virtual void changeEvent(QEvent *e); + +private slots: + void onFont(); + void onColor(); + void textChanged(); + void fontChanged(); + void colorChanged(); + void posChanged(); + void radiChanged(); + void angleChanged(); + void alignChanged(); + void locChanged(); + +private: + Ui::QDialogRoundText *m_ui; + QGraphicsRoundTextItem *m_item; +}; + +#endif // QDIALOGROUNDTEXT_H diff --git a/src/qdialogroundtext.ui b/src/qdialogroundtext.ui new file mode 100644 index 0000000..b1abe14 --- /dev/null +++ b/src/qdialogroundtext.ui @@ -0,0 +1,368 @@ + + QDialogRoundText + + + Qt::NonModal + + + + 0 + 0 + 537 + 738 + + + + Text properties + + + true + + + + + + + + QFrame::StyledPanel + + + QFrame::Raised + + + 3 + + + + + + + + + + X: + + + + + + + 1 + + + -61.000000000000000 + + + 61.000000000000000 + + + + + + + Y: + + + + + + + 1 + + + -61.000000000000000 + + + 61.000000000000000 + + + + + + + 10.000000000000000 + + + 80.000000000000000 + + + 20.000000000000000 + + + + + + + Radius: + + + + + + + 1 + + + 360.000000000000000 + + + 10.000000000000000 + + + + + + + Angle: + + + + + + + + + Qt::Horizontal + + + + 13 + 20 + + + + + + + + QFormLayout::ExpandingFieldsGrow + + + + + Font: + + + + + + + Color: + + + + + + + 0 + + + + Left + + + + + Center + + + + + Right + + + + + + + + Alignment: + + + + + + + 0 + + + + Inside + + + + + Outside + + + + + + + + Location: + + + + + + + false + + + + + + + + 20 + 20 + + + + + 20 + 20 + + + + QFrame::Box + + + + + + + + + + + + Qt::Horizontal + + + + 17 + 20 + + + + + + + + + + Font... + + + + + + + Color... + + + + + + + + + + + + + Text: + + + + + + + + + + + + + 0 + 1 + + + + false + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + + QCDView + QGraphicsView +
qcdview.h
+
+
+ + btnColor + buttonBox + spinX + spinY + spinRadi + spinAngle + comboAlignment + comboLocation + btnFont + + + + + buttonBox + accepted() + QDialogRoundText + accept() + + + 258 + 582 + + + 157 + 274 + + + + + buttonBox + rejected() + QDialogRoundText + reject() + + + 326 + 582 + + + 286 + 274 + + + + +
diff --git a/src/qdialogtext.cpp b/src/qdialogtext.cpp new file mode 100644 index 0000000..88221fe --- /dev/null +++ b/src/qdialogtext.cpp @@ -0,0 +1,133 @@ +/* qlscribe - Qt based application to print lightScribe discs + + Copyright (C) 2009 Vyacheslav Kononenko + + 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 + + $Id:$ */ + +#include "qdialogtext.h" +#include "ui_qdialogtext.h" +#include "qcdscene.h" + +#include +#include +#include + +QDialogText::QDialogText(QWidget *parent) : + QItemDialog(parent), + m_ui(new Ui::QDialogText), + m_item( 0 ) +{ + m_ui->setupUi(this); + connect( m_ui->btnFont, SIGNAL(clicked()), this, SLOT(onFont()) ); + connect( m_ui->btnColor, SIGNAL(clicked()), this, SLOT(onColor()) ); +} + +QDialogText::~QDialogText() +{ + delete m_ui; +} + +void QDialogText::changeEvent(QEvent *e) +{ + switch (e->type()) { + case QEvent::LanguageChange: + m_ui->retranslateUi(this); + break; + default: + break; + } +} + +bool QDialogText::exec( QGraphicsItem *graphicsItem ) +{ + QGraphicsSimpleTextItem *item = dynamic_cast< QGraphicsSimpleTextItem * >( graphicsItem ); + if( !item ) + return false; + + QCDScene scene; + m_ui->cdView->setScene( &scene ); + m_item = new QGraphicsSimpleTextItem; + + m_item->setPos( item->pos() ); + m_item->setText( item->text() ); + m_item->setFont( item->font() ); + m_item->setBrush( item->brush() ); + + scene.addItem( m_item ); + + m_ui->spinX->setValue( m_item->pos().x() ); + m_ui->spinY->setValue( m_item->pos().y() ); + m_ui->textEdit->setPlainText( m_item->text() ); + + colorChanged(); + fontChanged(); + + connect( m_ui->textEdit, SIGNAL(textChanged()), this, SLOT(textChanged()) ); + connect( m_ui->spinX, SIGNAL(valueChanged(double)), this, SLOT(posChanged()) ); + connect( m_ui->spinY, SIGNAL(valueChanged(double)), this, SLOT(posChanged()) ); + + if( QDialog::exec() == Rejected ) return false; + + item->setPos( m_item->pos() ); + item->setText( m_item->text() ); + item->setFont( m_item->font() ); + item->setBrush( m_item->brush() ); + + return true; +} + +void QDialogText::onFont() +{ + bool ok = true; + QFont font = QFontDialog::getFont( &ok, m_item->font(), this ); + if( ok ) { + m_item->setFont( font ); + fontChanged(); + } +} + +void QDialogText::onColor() +{ + QColor color = QColorDialog::getColor( m_item->brush().color(), this ); + if( color.isValid() ){ + m_item->setBrush( color ); + colorChanged(); + } +} + +void QDialogText::fontChanged() +{ + m_ui->fontName->setCurrentFont( m_item->font() ); +} + +void QDialogText::colorChanged() +{ + QPixmap pixmap( m_ui->colorWidget->size() ); + pixmap.fill( m_item->brush().color() ); + m_ui->colorWidget->setPixmap( pixmap ); +} + +void QDialogText::textChanged() +{ + m_item->setText( m_ui->textEdit->toPlainText() ); +} + +void QDialogText::posChanged() +{ + m_item->setPos( m_ui->spinX->value(), m_ui->spinY->value() ); +} + diff --git a/src/qdialogtext.h b/src/qdialogtext.h new file mode 100644 index 0000000..e0620d4 --- /dev/null +++ b/src/qdialogtext.h @@ -0,0 +1,57 @@ +/* qlscribe - Qt based application to print lightScribe discs + + Copyright (C) 2009 Vyacheslav Kononenko + + 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 + + $Id:$ */ + +#ifndef QDIALOGTEXT_H +#define QDIALOGTEXT_H + +#include "qshapefactory.h" + +namespace Ui { + class QDialogText; +} + +class QGraphicsSimpleTextItem; + +class QDialogText : public QItemDialog { + Q_OBJECT + Q_DISABLE_COPY(QDialogText) +public: + explicit QDialogText(QWidget *parent); + virtual ~QDialogText(); + + virtual bool exec( QGraphicsItem *item ); + +protected: + virtual void changeEvent(QEvent *e); + +private slots: + void onFont(); + void onColor(); + void textChanged(); + void fontChanged(); + void colorChanged(); + void posChanged(); + +private: + Ui::QDialogText *m_ui; + QGraphicsSimpleTextItem *m_item; +}; + +#endif // QDIALOGTEXT_H diff --git a/src/qdialogtext.ui b/src/qdialogtext.ui new file mode 100644 index 0000000..169f2e2 --- /dev/null +++ b/src/qdialogtext.ui @@ -0,0 +1,311 @@ + + QDialogText + + + Qt::NonModal + + + + 0 + 0 + 461 + 627 + + + + Text properties + + + false + + + + + + + + QFrame::StyledPanel + + + QFrame::Raised + + + 3 + + + + + + QLayout::SetMaximumSize + + + 0 + + + + + + + X: + + + + + + + 1 + + + -61.000000000000000 + + + 61.000000000000000 + + + + + + + Y: + + + + + + + 1 + + + -61.000000000000000 + + + 61.000000000000000 + + + + + + + + + Qt::Horizontal + + + + 13 + 20 + + + + + + + + + + Font: + + + + + + + Color: + + + + + + + false + + + false + + + + + + + + 20 + 20 + + + + + 20 + 20 + + + + + 0 + 0 + + + + QFrame::Box + + + + + + Qt::PlainText + + + + + + + + + Qt::Horizontal + + + + 17 + 20 + + + + + + + + QFormLayout::ExpandingFieldsGrow + + + + + Color... + + + + + + + Font... + + + + + + + + + + + + + Text + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + + + + + + + 0 + 0 + + + + + 16777215 + 50 + + + + + + + + + + + 0 + 2 + + + + + 0 + 0 + + + + false + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + + QCDView + QGraphicsView +
qcdview.h
+
+
+ + spinX + spinY + btnFont + btnColor + buttonBox + + + + + buttonBox + accepted() + QDialogText + accept() + + + 258 + 582 + + + 157 + 274 + + + + + buttonBox + rejected() + QDialogText + reject() + + + 326 + 582 + + + 286 + 274 + + + + +
diff --git a/src/qgraphicsroundtextitem.cpp b/src/qgraphicsroundtextitem.cpp new file mode 100644 index 0000000..a8192a7 --- /dev/null +++ b/src/qgraphicsroundtextitem.cpp @@ -0,0 +1,295 @@ +/* qlscribe - Qt based application to print lightScribe discs + + Copyright (C) 2009 Vyacheslav Kononenko + + 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 + + $Id:$ */ + +#include "qgraphicsroundtextitem.h" +#include "qdialogroundtext.h" + +#include +#include +#include +#include +#include + +#include + +QGraphicsRoundTextItem::QGraphicsRoundTextItem( QGraphicsItem *parent ) + : QAbstractGraphicsShapeItem( parent ), + m_radius( 40.0 ), + m_angle( 0.0 ), + m_alignment( Qt::AlignLeft ), + m_outside( true ) +{ +} + +const QFont & QGraphicsRoundTextItem::font() const +{ + return m_font; +} + +const QString & QGraphicsRoundTextItem::text() const +{ + return m_text; +} + +double QGraphicsRoundTextItem::radius() const +{ + return m_radius; +} + +double QGraphicsRoundTextItem::angle() const +{ + return m_angle; +} + +Qt::Alignment QGraphicsRoundTextItem::alignment() const +{ + return m_alignment; +} + +bool QGraphicsRoundTextItem::outside() const +{ + return m_outside; +} + +void QGraphicsRoundTextItem::setFont( const QFont &font ) +{ + prepareGeometryChange(); + m_font = font; +} + +void QGraphicsRoundTextItem::setText( const QString &text ) +{ + prepareGeometryChange(); + m_text = text; +} + +void QGraphicsRoundTextItem::setRadius( double radius ) +{ + prepareGeometryChange(); + m_radius = radius; +} + +void QGraphicsRoundTextItem::setAngle( double angle ) +{ + prepareGeometryChange(); + m_angle = angle; +} + +void QGraphicsRoundTextItem::setAlignment( Qt::Alignment alignment ) +{ + prepareGeometryChange(); + m_alignment = alignment; +} + +void QGraphicsRoundTextItem::setOutside( bool outside ) +{ + prepareGeometryChange(); + m_outside = outside; +} + +QRectF QGraphicsRoundTextItem::boundingRect() const +{ + double height = m_radius; + if( m_outside ) { + QFontMetrics mcs( m_font ); + height += ( mcs.height() * 2 ) / 3; + } + height += 1; // pad + return QRectF( -height, -height, 2 * height, 2 * height ); +} + +QPainterPath QGraphicsRoundTextItem::shape() const +{ + QFontMetrics mcs( m_font ); + double arcAngle = 0.0; + const double pad = 1; + + for( int i = 0; i < m_text.size(); ++i ) + arcAngle += mcs.width( m_text[ i ] ); + + double height = ( mcs.height() * 2 ) / 3; + double in, out; + if( m_outside ) { + in = m_radius - pad; + out = m_radius + height + pad; + } else { + in = m_radius - height - pad; + out = m_radius + pad; + } + arcAngle /= in * 3.14159 / 180.0; + double padAngle = pad * 180.0 / ( in * 3.14159 ); + + int sign = m_outside ? 1 : -1; + + double angle = m_angle; + switch( m_alignment ) { + case Qt::AlignCenter : + case Qt::AlignHCenter : + angle -= sign * arcAngle / 2; + break; + case Qt::AlignRight : + angle -= sign * arcAngle; + break; + } + + QPainterPath path; + double radAngle = ( angle - sign * padAngle ) * 3.14159 / 180.0; + path.moveTo( sign * out * sin( radAngle ), -sign * out * cos( radAngle ) ); + path.arcTo( -out, -out, out * 2, out * 2, + sign * ( 90 + padAngle ) - angle, + -sign * ( arcAngle + 2 * padAngle ) ); + path.arcTo( -in, -in, in * 2, in * 2, + sign * ( 90 - padAngle - arcAngle ) - angle, + sign * ( arcAngle + 2 * padAngle ) ); + path.closeSubpath(); + + return path; +} + + +void QGraphicsRoundTextItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget ) +{ + Q_UNUSED( option ); + Q_UNUSED( widget ); + if( !painter || !m_radius ) return; + painter->save(); + + painter->setFont( m_font ); + + //painter->drawEllipse( - m_radius, - m_radius, m_radius * 2, m_radius * 2 ); + + QFontMetrics mcs( m_font ); + + const double radi = m_radius - ( m_outside ? 0 : mcs.xHeight() ); + const double diam = 2 * radi; + double angle = m_angle * 3.14159 / 180.0; + switch( m_alignment ) { + case Qt::AlignCenter : + case Qt::AlignHCenter : + angle += mcs.width( m_text ) / diam * ( m_outside ? -1 : 1 ); + break; + case Qt::AlignRight : + angle += mcs.width( m_text ) / radi * ( m_outside ? -1 : 1 ); + break; + } + + QTransform trans; + trans.rotateRadians( angle ); + painter->setWorldTransform( trans, true ); + + int prevWidth = 0; + for( int i = 0; i < m_text.size(); ++i ) { + QChar ch = m_text[ i ]; + + double d = ( prevWidth + mcs.width( ch ) ) / diam; + trans.reset(); + trans.rotateRadians( m_outside ? d : -d ); + painter->setWorldTransform( trans, true ); + prevWidth = mcs.width( ch ); + + painter->drawText( -prevWidth / 2.0, m_outside ? -m_radius : m_radius, ch ); + } + painter->restore(); + if (option->state & QStyle::State_Selected) { + painter->setPen( QPen( option->palette.windowText(), 0, Qt::DashLine ) ); + painter->setBrush(Qt::NoBrush); + painter->drawPath( shape() ); + } +} + +RegisterController< QShapeControllerRoundText > regControllerRoundText; + +QString QShapeControllerRoundText::name() const +{ + return "roundText"; +} + +QString QShapeControllerRoundText::menuName() const +{ + return QObject::tr( "Round Text", "QShapeControllerRoundText name" ); +} + +QGraphicsItem *QShapeControllerRoundText::create() const +{ + return new QGraphicsRoundTextItem; +} + +QItemDialog *QShapeControllerRoundText::createDialog( QWidget *parent ) const +{ + return new QDialogRoundText( parent ); +} + +void QShapeControllerRoundText::writeData( QXmlStreamWriter &writer, const QGraphicsItem *item ) const +{ + const QGraphicsRoundTextItem *textItem = static_cast< const QGraphicsRoundTextItem * >( item ); + writer.writeEmptyElement( "pos" ); + writer.writeAttribute( QXmlStreamAttribute( "x", QString::number( textItem->pos().x() ) ) ); + writer.writeAttribute( QXmlStreamAttribute( "y", QString::number( textItem->pos().y() ) ) ); + + writer.writeTextElement( "radius", QString::number( textItem->radius() ) ); + writer.writeTextElement( "angle", QString::number( textItem->angle() ) ); + writer.writeTextElement( "alignment", QString::number( textItem->alignment() ) ); + writer.writeTextElement( "location", QString::number( textItem->outside() ) ); + writer.writeTextElement( "font", textItem->font().toString() ); + writer.writeTextElement( "color", textItem->brush().color().name() ); + writer.writeTextElement( "text", textItem->text() ); +} + +void QShapeControllerRoundText::readData( const QString &element, + const QXmlStreamAttributes &attrs, + const QString &data, + QGraphicsItem *item ) const +{ + QGraphicsRoundTextItem *textItem = static_cast< QGraphicsRoundTextItem * >( item ); + + if( element == "pos" ) { + textItem->setPos( attrs.value( "x" ).toString().toDouble(), + attrs.value( "y" ).toString().toDouble() ); + return; + } + + if( element == "font" ) { + QFont font; + font.fromString( data ); + textItem->setFont( font ); + return; + } + + if( element == "color" ) { + textItem->setBrush( QColor( data ) ); + return; + } + + if( element == "text" ) { + textItem->setText( data ); + return; + } + + if( element == "location" ) { + textItem->setOutside( data.toInt() ); + return; + } + + if( element == "alignment" ) { + textItem->setAlignment( Qt::Alignment( data.toInt() ) ); + return; + } +} + diff --git a/src/qgraphicsroundtextitem.h b/src/qgraphicsroundtextitem.h new file mode 100644 index 0000000..2b52d65 --- /dev/null +++ b/src/qgraphicsroundtextitem.h @@ -0,0 +1,84 @@ +/* qlscribe - Qt based application to print lightScribe discs + + Copyright (C) 2009 Vyacheslav Kononenko + + 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 + + $Id:$ */ + +#ifndef QGRAPHICSROUNDTEXTITEM_H +#define QGRAPHICSROUNDTEXTITEM_H + +#include "qshapefactory.h" + +#include +#include + +class QGraphicsRoundTextItem : public QAbstractGraphicsShapeItem { +public: + enum { Type = UserType + 1 }; + + QGraphicsRoundTextItem( QGraphicsItem * parent = 0 ); + + const QFont &font() const; + const QString &text() const; + double radius() const; + double angle() const; + Qt::Alignment alignment() const; + bool outside() const; + + void setFont( const QFont &font ); + void setText( const QString &text ); + void setRadius( double radius ); + void setAngle( double angle ); + void setAlignment( Qt::Alignment alignment ); + void setOutside( bool outside ); + + virtual QRectF boundingRect() const; + virtual QPainterPath shape () const; + virtual void paint( QPainter *painter, + const QStyleOptionGraphicsItem *option, + QWidget *widget ); + + virtual int type() const { return Type; } +private: + QFont m_font; + QString m_text; + double m_radius; + double m_angle; + Qt::Alignment m_alignment; + bool m_outside; +}; + +class QShapeControllerRoundText : public QShapeController { +public: + enum { Type = QGraphicsRoundTextItem::Type }; + + virtual QString name() const; + virtual QString menuName() const; + + virtual QGraphicsItem *create() const; + +protected: + virtual QItemDialog *createDialog( QWidget *parent ) const; + virtual void writeData( QXmlStreamWriter &writer, const QGraphicsItem *item ) const; + virtual void readData( const QString &element, + const QXmlStreamAttributes &attrs, + const QString &data, + QGraphicsItem *item ) const; +}; + + +#endif // QGRAPHICSROUNDTEXTITEM_H diff --git a/src/qlscribe.pro b/src/qlscribe.pro new file mode 100644 index 0000000..5ef870f --- /dev/null +++ b/src/qlscribe.pro @@ -0,0 +1,27 @@ +TEMPLATE = app +LANGUAGE = C++ +CONFIG += qt +QMAKE_CXXFLAGS += -m32 +QMAKE_LFLAGS += -m32 +QMAKE_LIBDIR = /usr/lib32 +SOURCES += main.cpp \ + mainwindow.cpp \ + qgraphicsroundtextitem.cpp \ + qcdview.cpp \ + qcdscene.cpp \ + qshapefactory.cpp \ + qshapecontrollers.cpp \ + qdialogtext.cpp \ + qdialogroundtext.cpp + +# QMAKE_LIBS += -llightscribe +HEADERS += mainwindow.h \ + qgraphicsroundtextitem.h \ + qcdview.h \ + qcdscene.h \ + qshapefactory.h \ + qshapecontrollers.h \ + qdialogtext.h \ + qdialogroundtext.h +FORMS += qdialogtext.ui \ + qdialogroundtext.ui diff --git a/src/qshapecontrollers.cpp b/src/qshapecontrollers.cpp new file mode 100644 index 0000000..d013ae0 --- /dev/null +++ b/src/qshapecontrollers.cpp @@ -0,0 +1,91 @@ +/* qlscribe - Qt based application to print lightScribe discs + + Copyright (C) 2009 Vyacheslav Kononenko + + 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 + + $Id:$ */ + +#include "qshapecontrollers.h" +#include "qdialogtext.h" + +#include +#include + +RegisterController< QShapeControllerText > regControllerText; + +QString QShapeControllerText::name() const +{ + return "text"; +} + +QString QShapeControllerText::menuName() const +{ + return QObject::tr( "Text", "QShapeControllerText name" ); +} + +QGraphicsItem *QShapeControllerText::create() const +{ + return new QGraphicsSimpleTextItem; +} + +QItemDialog *QShapeControllerText::createDialog( QWidget *parent ) const +{ + return new QDialogText( parent ); +} + +void QShapeControllerText::writeData( QXmlStreamWriter &writer, const QGraphicsItem *item ) const +{ + const QGraphicsSimpleTextItem *textItem = static_cast< const QGraphicsSimpleTextItem * >( item ); + writer.writeEmptyElement( "pos" ); + writer.writeAttribute( QXmlStreamAttribute( "x", QString::number( textItem->pos().x() ) ) ); + writer.writeAttribute( QXmlStreamAttribute( "y", QString::number( textItem->pos().y() ) ) ); + + writer.writeTextElement( "font", textItem->font().toString() ); + writer.writeTextElement( "color", textItem->brush().color().name() ); + writer.writeTextElement( "text", textItem->text() ); +} + + +void QShapeControllerText::readData( const QString &element, + const QXmlStreamAttributes &attrs, + const QString &data, + QGraphicsItem *item ) const +{ + QGraphicsSimpleTextItem *textItem = static_cast< QGraphicsSimpleTextItem * >( item ); + + if( element == "pos" ) { + textItem->setPos( attrs.value( "x" ).toString().toDouble(), + attrs.value( "y" ).toString().toDouble() ); + return; + } + + if( element == "font" ) { + QFont font; + font.fromString( data ); + textItem->setFont( font ); + return; + } + + if( element == "color" ) { + textItem->setBrush( QColor( data ) ); + return; + } + + if( element == "text" ) { + textItem->setText( data ); + return; + } +} diff --git a/src/qshapecontrollers.h b/src/qshapecontrollers.h new file mode 100644 index 0000000..5e58f6d --- /dev/null +++ b/src/qshapecontrollers.h @@ -0,0 +1,46 @@ +/* qlscribe - Qt based application to print lightScribe discs + + Copyright (C) 2009 Vyacheslav Kononenko + + 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 + + $Id:$ */ + +#ifndef QSHAPECONTROLLERS_H +#define QSHAPECONTROLLERS_H + +#include "qshapefactory.h" + +#include + +class QShapeControllerText : public QShapeController { +public: + enum { Type = QGraphicsSimpleTextItem::Type }; + + virtual QString name() const; + virtual QString menuName() const; + + virtual QGraphicsItem *create() const; + +protected: + virtual QItemDialog *createDialog( QWidget *parent ) const; + virtual void writeData( QXmlStreamWriter &writer, const QGraphicsItem *item ) const; + virtual void readData( const QString &element, + const QXmlStreamAttributes &attrs, + const QString &data, + QGraphicsItem *item ) const; +}; + +#endif // QSHAPECONTROLLERS_H diff --git a/src/qshapefactory.cpp b/src/qshapefactory.cpp new file mode 100644 index 0000000..cf0149a --- /dev/null +++ b/src/qshapefactory.cpp @@ -0,0 +1,144 @@ +/* qlscribe - Qt based application to print lightScribe discs + + Copyright (C) 2009 Vyacheslav Kononenko + + 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 + + $Id:$ */ + +#include "qshapefactory.h" + +#include +#include +#include + +QShapeController::~QShapeController() +{ +} + +bool QShapeController::edit( QGraphicsItem *item, QWidget *parent ) const +{ + QItemDialog *dialog = createDialog( parent ); + if( !dialog ) + return false; + + bool rez = false; + try { + rez = dialog->exec( item ) == QDialog::Accepted; + } + catch(...) { + delete dialog; + throw; + } + delete dialog; + return rez; +} + +void QShapeController::write( QXmlStreamWriter &writer, const QGraphicsItem *item ) const +{ + writer.writeStartElement( "item" ); + writer.writeAttribute( QXmlStreamAttribute( "type", name() ) ); + writeData( writer, item ); + writer.writeEndElement(); +} + +QGraphicsItem *QShapeController::read( QXmlStreamReader &reader ) const +{ + QGraphicsItem *item = create(); + try { + while( true ) { + QXmlStreamReader::TokenType ttype = reader.readNext(); + + if( reader.atEnd() ) + throw QString( "QShapeController: unexpected end of document" ); + + if( ttype == QXmlStreamReader::EndElement ) + break;; + + if( ttype == QXmlStreamReader::StartElement ) { + QString elementName = reader.name().toString(); + QXmlStreamAttributes attributes = reader.attributes(); + QString data = reader.readElementText(); + + readData( elementName, attributes, data, item ); + } + } + } + catch(...) { + delete item; + throw; + } + return item; +} + +QShapeFactory &QShapeFactory::QShapeFactory::instance() +{ + static QShapeFactory inst; + return inst; +} + +QShapeFactory::QShapeFactory() +{ +} + +QShapeFactory::~QShapeFactory() +{ + for( iterator i = begin(); i != end(); ++i ) + delete i->second; +} + + +QString QShapeFactory::name( int type ) const +{ + QShapeController *ctrl = getController( type ); + return ctrl ? ctrl->name() : QString(); +} + +QGraphicsItem *QShapeFactory::create( int type ) const +{ + QShapeController *ctrl = getController( type ); + return ctrl ? ctrl->create() : 0; +} + +bool QShapeFactory::edit( QGraphicsItem *item, QWidget *parent ) const +{ + QShapeController *ctrl = getController( item->type() ); + if( ctrl ) + return ctrl->edit( item, parent ); + + return false; +} + +QShapeController *QShapeFactory::getController( int type ) const +{ + Controllers::const_iterator f = m_controllers.find( type ); + if( f == m_controllers.end() ) { + QMessageBox::warning( 0, "Warning", QObject::tr( "Cannot find controller for type %n", 0, type ) ); + return 0; + } + return f->second; +} + +QShapeFactory::iterator QShapeFactory::find( const QString &name ) const +{ + Name2Type::const_iterator f = m_name2type.find( name ); + return f == m_name2type.end() ? end() : find( f->second ); +} + +void QShapeFactory::registerController( int type, QShapeController *ctrl ) +{ + m_controllers.insert( std::make_pair( type, ctrl ) ); + m_name2type.insert( std::make_pair( ctrl->name(), type ) ); +} diff --git a/src/qshapefactory.h b/src/qshapefactory.h new file mode 100644 index 0000000..b8b1f58 --- /dev/null +++ b/src/qshapefactory.h @@ -0,0 +1,96 @@ +/* qlscribe - Qt based application to print lightScribe discs + + Copyright (C) 2009 Vyacheslav Kononenko + + 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 + + $Id:$ */ + +#ifndef QSHAPEFACTORY_H +#define QSHAPEFACTORY_H + +#include + +#include +#include + +class QGraphicsItem; +class QXmlStreamAttributes; + +class QItemDialog : public QDialog { +public: + QItemDialog( QWidget *parent ) : QDialog( parent ) {} + + virtual bool exec( QGraphicsItem *item ) = 0; +}; + +class QXmlStreamWriter; +class QXmlStreamReader; + +class QShapeController { +public: + virtual ~QShapeController(); + + virtual QString name() const = 0; + virtual QString menuName() const = 0; + virtual QGraphicsItem *create() const = 0; + bool edit( QGraphicsItem *item, QWidget *parent ) const; + void write( QXmlStreamWriter &writer, const QGraphicsItem *item ) const; + QGraphicsItem *read( QXmlStreamReader &reader ) const; + +protected: + virtual QItemDialog *createDialog( QWidget *parent ) const = 0; + virtual void writeData( QXmlStreamWriter &writer, const QGraphicsItem *item ) const = 0; + virtual void readData( const QString &element, + const QXmlStreamAttributes &attrs, + const QString &data, + QGraphicsItem *item ) const = 0; +}; + +class QShapeFactory { +public: + typedef std::map< int, QShapeController * > Controllers; + typedef Controllers::const_iterator iterator; + static QShapeFactory &instance(); + + void registerController( int type, QShapeController *ctrl ); + + QString name( int type ) const; + QGraphicsItem *create( int type ) const; + bool edit( QGraphicsItem *item, QWidget *parent ) const; + + iterator find( const QString &name ) const; + iterator find( int type ) const { return m_controllers.find( type ); } + iterator begin() const { return m_controllers.begin(); } + iterator end() const { return m_controllers.end(); } + +private: + QShapeFactory(); + ~QShapeFactory(); + + QShapeController *getController( int type ) const; + + Controllers m_controllers; + + typedef std::map< QString, int > Name2Type; + Name2Type m_name2type; +}; + +template +struct RegisterController { + RegisterController() { QShapeFactory::instance().registerController( T::Type, new T ); } +}; + +#endif // QSHAPEFACTORY_H