diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index e3d2b35..a34f01a 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -41,6 +41,7 @@ #include #include #include +#include MainWindow::MainWindow( bool enablePrint ) @@ -320,18 +321,23 @@ void MainWindow::onMenuInsert( int id ) return; } - item->setFlag( QGraphicsItem::ItemIsMovable, true ); - item->setFlag( QGraphicsItem::ItemIsSelectable, true ); cdscene->addItem( item ); cdscene->setChanged(); } void MainWindow::onMenuOpen() { + QString filter( tr("qlscribe document (*.qlx)\nImages ( ") ); + QList list = QImageReader::supportedImageFormats(); + foreach( QByteArray arr, list ) { + filter += QString( "*." ) + arr.data() + " "; + } + filter += tr( ")\nAll Files (*)" ); + QString fileName = QFileDialog::getOpenFileName( this, tr( "Open:" ), QString(), - tr("qlscribe document (*.qlx)") ); + filter ); if( fileName.isNull() ) return; diff --git a/src/qcdscene.cpp b/src/qcdscene.cpp index 85b55d1..30ea86d 100644 --- a/src/qcdscene.cpp +++ b/src/qcdscene.cpp @@ -20,6 +20,7 @@ #include "qcdscene.h" #include "qshapefactory.h" +#include "qlightpixmapitem.h" #include #include @@ -34,6 +35,7 @@ #include #include #include +#include QCDScene::QCDScene( QObject * parent ) : QGraphicsScene( parent ), @@ -82,30 +84,52 @@ void QCDScene::start( LabelMode mode ) bool QCDScene::load( const QString &fileName, QString *errMessage ) { QFile file( fileName ); - if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) { - if( errMessage ) - *errMessage = "cannot open file for reading"; - else - QMessageBox::critical( 0, tr( "Error" ), tr( "Cannot open file for reading\n" ) + fileName ); - return false; - } - QXmlStreamReader reader( &file ); - try { - read( reader ); - } - catch( const QString &err ) { - if( errMessage ) - *errMessage = err; - else - QMessageBox::critical( 0, tr( "Error" ), tr( "Cannot read file " ) + fileName + "\n" + err ); - return false; - } - m_fileName = fileName; - m_saved = true; - setName(); + if( QRegExp( "*.\\.qlx", Qt::CaseInsensitive ).exactMatch( fileName ) ) { + if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) { + if( errMessage ) + *errMessage = "cannot open file for reading"; + else + QMessageBox::critical( 0, tr( "Error" ), tr( "Cannot open file for reading\n" ) + fileName ); + return false; + } + QXmlStreamReader reader( &file ); + try { + read( reader ); + } + catch( const QString &err ) { + if( errMessage ) + *errMessage = err; + else + QMessageBox::critical( 0, tr( "Error" ), tr( "Cannot read file " ) + fileName + "\n" + err ); + return false; + } + m_fileName = fileName; + m_saved = true; + setName(); - pushUndo(); - return true; + pushUndo(); + return true; + } + QShapeFactory &sfactory = QShapeFactory::instance(); + QLightPixmapItem *item = + static_cast(sfactory.create( QShapeControllerPixmap::Type ) ); + addItem( item ); + if( item->loadImage( fileName ) ) { + QSize size = item->pixmap().size(); + item->setOffset( -QPointF( size.height(), size.width() ) / 2.0 ); + item->setTransform( QTransform().scale( size.width() ? 120.0 / size.width() : 1.0, + size.height() ? 120.0 / size.height() : 1.0 ) ); + setName(); + pushUndo(); + return true; + } else { + if( errMessage ) + *errMessage = "cannot load image"; + else + QMessageBox::critical( 0, tr( "Error" ), tr( "Cannot load image " ) + + fileName + "\n" ); + } + return false; } diff --git a/src/qlightpixmapitem.cpp b/src/qlightpixmapitem.cpp index 034a0c2..c4d4442 100644 --- a/src/qlightpixmapitem.cpp +++ b/src/qlightpixmapitem.cpp @@ -29,6 +29,17 @@ QLightPixmapItem::QLightPixmapItem() { } +bool QLightPixmapItem::loadImage( const QString &name ) +{ + QPixmap pixmap( name ); + if( pixmap.isNull() ) + return false; + m_imageName = name; + setPixmap( pixmap ); + return true; +} + + QVariant QLightPixmapItem::itemChange( GraphicsItemChange change, const QVariant & value ) { if( scene() && change == ItemPositionHasChanged ) diff --git a/src/qlightpixmapitem.h b/src/qlightpixmapitem.h index d9e2ceb..f4945df 100644 --- a/src/qlightpixmapitem.h +++ b/src/qlightpixmapitem.h @@ -25,11 +25,11 @@ #include "qshapefactory.h" -class QLightPixmapItem : public QGraphicsPixmapItem -{ +class QLightPixmapItem : public QGraphicsPixmapItem { public: QLightPixmapItem(); + bool loadImage( const QString &name ); void imageName( const QString &name ) { m_imageName = name; } const QString &imageName() const { return m_imageName; } diff --git a/src/qshapefactory.cpp b/src/qshapefactory.cpp index 81e1344..5712e75 100644 --- a/src/qshapefactory.cpp +++ b/src/qshapefactory.cpp @@ -118,7 +118,13 @@ QString QShapeFactory::name( int type ) const QGraphicsItem *QShapeFactory::create( int type ) const { QShapeController *ctrl = getController( type ); - return ctrl ? ctrl->create() : 0; + if( !ctrl ) return 0; + QGraphicsItem *item = ctrl->create(); + if( item ) { + item->setFlag( QGraphicsItem::ItemIsMovable, true ); + item->setFlag( QGraphicsItem::ItemIsSelectable, true ); + } + return item; } bool QShapeFactory::edit( QGraphicsItem *item, QWidget *parent ) const