diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index adf1e43..2694546 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,14 +21,14 @@ SET( QLSCRIBE_SRCS main.cpp mainwindow.cpp qcdscene.cpp qcdview.cpp qdialogpixmap.cpp qdialogroundtext.cpp qdialogtext.cpp qlightroundtextitem.cpp qlightscribe.cpp qshapefactory.cpp qlighttextitem.cpp qlightpixmapitem.cpp - qdialogprint.cpp qdialogprogress.cpp qdialogcdproperties.cpp) + qdialogprint.cpp qdialogprogress.cpp qdialogcdproperties.cpp qconsoleprintprogress.cpp) SET( QLSCRIBE_UIS qdialogtext.ui qdialogroundtext.ui qdialogpixmap.ui qdialogprint.ui qdialogprogress.ui qdialogcdproperties.ui) SET( QLSCRIBE_MOC_HDRS mainwindow.h qcdscene.h qdialogpixmap.h qdialogroundtext.h qdialogtext.h qlightscribe.h qdialogprint.h qdialogprogress.h - qdialogcdproperties.h) + qdialogcdproperties.h qconsoleprintprogress.h) ADD_DEFINITIONS( -Wall ) diff --git a/src/main.cpp b/src/main.cpp index 7d5f34c..adccaba 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,34 +20,138 @@ #include #include +#include #include "mainwindow.h" +#include "qcdscene.h" +#include "qlightscribe.h" +#include "qconsoleprintprogress.h" + +#include uid_t realUserId; -int main(int argc, char **argv) +std::ostream &operator<<( std::ostream &os, const QString &str ) { - QApplication app(argc, argv); + return os << str.toAscii().data(); +} + +void usage() +{ + std::cout << "Usage: \n" + << "To run GUI: qlscribe [filename] [filename] ...\n" + << "To print label in console mode: qlscribe --print [--drive Index] [--replace KEY=VALUE] filename\n" + << "\tParameters:\n" + << "\t\t--help | -h - print this message\n" + << "\t\t--print | -p - switch to print mode\n" + << "\t\t--drive | -d NUMBER - use this drive, starts from 0, 0 is default\n" + << "\t\t--replace | -r KEY=VALUE - replace text from KEY to VALUE\n" + << std::endl; +} + +int main( int argc, char **argv ) +{ + QApplication app( argc, argv ); app.addLibraryPath( "/usr/lib32/qt4/plugins" ); + bool doPrint = false; + QStringList arguments = app.arguments(); + QStringList files; + QCDScene::QString2String replacements; + int driveIndex = 0; + QLightScribe::PrintParameters params; + bool labelModeOverriden = false; + + for( int i = 1; i < arguments.size(); ++i ) { + QString arg = arguments[i]; + if( arg == "--print" || arg == "-p" ) { + doPrint = true; + continue; + } + if( arg == "--help" || arg == "-h" ) { + usage(); + return 0; + } + if( arg == "--replace" || arg == "-r" ) { + if( i == arguments.size() - 1 ) { + std::cerr << "Error: arg#" << i + << " argument expected for \"" << arg << "\" not enough parameters" << std::endl; + return 1; + } + QString param = arguments[ ++i ]; + int pos = param.indexOf( '=' ); + if( pos == -1 ) { + std::cerr << "Error: arg#" << i + << " invalid argument for replacement, KEY=VALUE expected, missing =" << std::endl; + return 2; + } + replacements[ param.left( pos ) ] = param.right( param.size() - pos - 1 ); + continue; + } + if( arg[0] != '-' ) + files.append( arg ); + } + + if( doPrint && files.size() != 1 ) { + if( files.size() ) + std::cerr << "Error: only one filename expected to print, got " << files.size() << " instead" << std::endl; + else + std::cerr << "Error: filename required to print" << std::endl; + return 3; + } + bool enablePrint = false; if( geteuid() ) { - if( QMessageBox::question( 0, - QObject::tr( "Confirmation" ), - QObject::tr( "Print functionality requires setuid (sticky) flag set on the application\n" - "This program does not seem to have it set, print functiionality will be disabled\n" - "You still will be able to do print preview and edit documents\n" - "Do you want to continue?" ), - QMessageBox::Yes | QMessageBox::No, - QMessageBox::No ) - == QMessageBox::No ) - return 1; + if( !doPrint ) { + if( QMessageBox::question( 0, + QObject::tr( "Confirmation" ), + QObject::tr( "Print functionality requires setuid (sticky) flag set on the application\n" + "This program does not seem to have it set, print functionality will be disabled\n" + "You still will be able to do print preview and edit documents\n" + "Do you want to continue?" ), + QMessageBox::Yes | QMessageBox::No, + QMessageBox::No ) + == QMessageBox::No ) + return 4; + } else { + std::cerr << "Error: setuid flag is not set, cannot print" << std::endl; + return 4; + } } else { realUserId = getuid(); setreuid( 0, realUserId ); enablePrint = true; } - MainWindow mwindow( enablePrint ); - mwindow.show(); - return app.exec(); + int rez = 0; + + if( doPrint ) { + // print here + QCDScene scene; + QString err; + if( !scene.load( files.front(), &err ) ) { + std::cerr << "Error: cannot read \"" << files.front() << "\" - " << err << std::endl; + return 5; + } + scene.replace( replacements ); + if( !labelModeOverriden ) + params.m_labelMode = scene.labelMode(); + + QLightScribe *scribe = QLightScribe::instance(); + QList< QLightDrive * > drives = scribe->getDrives(); + if( driveIndex >= drives.size() ) { + std::cerr << "Error: drive " << driveIndex << " but there are only " << drives.size() << " drives" << std::endl; + return 6; + } + QConsolePrintProgress progress; + std::cout << "Printing label " << files.front() << std::endl; + scribe->print( drives[driveIndex], params, &scene ); + rez = app.exec(); + } else { + MainWindow mwindow( enablePrint ); + mwindow.show(); + mwindow.open( files ); + rez = app.exec(); + } + + return rez; } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 96e8ca4..c68891a 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -131,9 +131,26 @@ MainWindow::MainWindow( bool enablePrint ) MainWindow::~MainWindow() { - QLightScribe *lscribe = QLightScribe::instance(); - lscribe->stopThread(); - lscribe->wait( 1000 ); + QLightScribe *scribe = QLightScribe::instance(); + scribe->stopThread(); + scribe->wait( 1000 ); +} + +void MainWindow::open( const QStringList &files ) +{ + foreach( QString fileName, files ) { + QCDView *newView = new QCDView; + QCDScene *scene = new QCDScene( newView ); + newView->setScene( scene ); + + if( !scene->load( fileName ) ) { + delete newView; + continue; + } + + QMdiSubWindow *subWindow = m_mdiArea->addSubWindow( newView ); + subWindow->show(); + } } bool MainWindow::saveSceneAs( QCDScene *scene ) diff --git a/src/mainwindow.h b/src/mainwindow.h index cde6583..593ad27 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -35,6 +35,8 @@ public: virtual ~MainWindow(); bool saveScene( QCDScene *scene ); + void open( const QStringList &files ); + protected: virtual void closeEvent( QCloseEvent *event ); diff --git a/src/qcdscene.cpp b/src/qcdscene.cpp index 454be01..ca0bdd3 100644 --- a/src/qcdscene.cpp +++ b/src/qcdscene.cpp @@ -48,11 +48,28 @@ QCDScene::~QCDScene() { } -bool QCDScene::load( const QString &fileName ) +void QCDScene::replace( const QString2String &strings ) +{ + QShapeFactory &sfactory = QShapeFactory::instance(); + QList list = items(); + + for( QString2String::const_iterator st = strings.begin(); st != strings.end(); ++st ) { + foreach(QGraphicsItem *item, list ) { + QShapeFactory::iterator f = sfactory.find( item->type() ); + if( f != sfactory.end() ) + f->second->replace( item, st.key(), st.value() ); + } + } +} + +bool QCDScene::load( const QString &fileName, QString *errMessage ) { QFile file( fileName ); if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) { - QMessageBox::critical( 0, tr( "Error" ), tr( "Cannot open file for reading\n" ) + fileName ); + 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 ); @@ -60,7 +77,10 @@ bool QCDScene::load( const QString &fileName ) read( reader ); } catch( const QString &err ) { - QMessageBox::critical( 0, tr( "Error" ), tr( "Cannot read file " ) + fileName + "\n" + err ); + if( errMessage ) + *errMessage = err; + else + QMessageBox::critical( 0, tr( "Error" ), tr( "Cannot read file " ) + fileName + "\n" + err ); return false; } m_fileName = fileName; @@ -162,13 +182,13 @@ void QCDScene::write( QXmlStreamWriter &writer ) 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() ); + foreach(QGraphicsItem *item, list ) { + QShapeFactory::iterator f = sfactory.find( item->type() ); if( f == sfactory.end() ) { - QMessageBox::warning( 0, tr( "Warning" ), tr( "Cannot find controller for type %n", 0, (*it)->type() ) ); + QMessageBox::warning( 0, tr( "Warning" ), tr( "Cannot find controller for type %n", 0, item->type() ) ); continue; } - f->second->write( writer, *it ); + f->second->write( writer, item ); } writer.writeEndElement(); diff --git a/src/qcdscene.h b/src/qcdscene.h index b239e91..9b5bd52 100644 --- a/src/qcdscene.h +++ b/src/qcdscene.h @@ -31,6 +31,8 @@ class QXmlStreamReader; class QCDScene : public QGraphicsScene { Q_OBJECT public: + typedef QMap QString2String; + QCDScene( QObject * parent = 0 ); virtual ~QCDScene(); @@ -45,7 +47,8 @@ public: void setChanged(); void setName(); - bool load( const QString &fileName ); + bool load( const QString &fileName, QString *errMessage = 0 ); + void replace( const QString2String &strings ); bool save(); bool saveAs( const QString &fileName ); diff --git a/src/qconsoleprintprogress.cpp b/src/qconsoleprintprogress.cpp new file mode 100644 index 0000000..699f67d --- /dev/null +++ b/src/qconsoleprintprogress.cpp @@ -0,0 +1,50 @@ +#include "qconsoleprintprogress.h" +#include "qlightscribe.h" + +#include + +#include + +QConsolePrintProgress::QConsolePrintProgress() +{ + QLightScribe *scribe = QLightScribe::instance(); + + connect( scribe, SIGNAL(prepareProgress(long,long)), this, SLOT(onPrepareProgress(long,long)) ); + connect( scribe, SIGNAL(labelProgress(long,long)), this, SLOT(onLabelProgress(long,long)) ); + connect( scribe, SIGNAL(timeEstimate(long)), this, SLOT(onTimeEstimate(long)) ); + connect( scribe, SIGNAL(finished(int)), this, SLOT(onFinished(int)) ); + +} + +QConsolePrintProgress::~QConsolePrintProgress() +{ +} + +void QConsolePrintProgress::onPrepareProgress( long current, long final ) +{ + std::cout << "Preparing label: " << current << '/' << final << " \r"; +} + +void QConsolePrintProgress::onLabelProgress( long current, long final ) +{ + std::cout << "Printing label: " << current << '/' << final << " \r"; +} + +void QConsolePrintProgress::onTimeEstimate( long time ) +{ + std::cout << std::endl; +} + +void QConsolePrintProgress::onFinished( int status ) +{ + std::cout << "\n"; + if( !status ) + std::cout << "Print successfull" << std::endl; + else + std::cout << "Prin failed: 0x" << QString::number( status, 16 ) << std::endl; + + QLightScribe *scribe = QLightScribe::instance(); + scribe->stopThread(); + scribe->wait( 1000 ); + qApp->exit( status ); +} diff --git a/src/qconsoleprintprogress.h b/src/qconsoleprintprogress.h new file mode 100644 index 0000000..02643d3 --- /dev/null +++ b/src/qconsoleprintprogress.h @@ -0,0 +1,19 @@ +#ifndef QCONSOLEPRINTPROGRESS_H +#define QCONSOLEPRINTPROGRESS_H + +#include + +class QConsolePrintProgress : public QObject { + Q_OBJECT +public: + QConsolePrintProgress(); + ~QConsolePrintProgress(); + +private slots: + void onPrepareProgress( long current, long final ); + void onLabelProgress( long current, long final ); + void onTimeEstimate( long time ); + void onFinished(int status ); +}; + +#endif // QCONSOLEPRINTPROGRESS_H diff --git a/src/qlightroundtextitem.cpp b/src/qlightroundtextitem.cpp index 2949cb7..27c0f8a 100644 --- a/src/qlightroundtextitem.cpp +++ b/src/qlightroundtextitem.cpp @@ -261,6 +261,13 @@ QItemDialog *QShapeControllerRoundText::createDialog( QWidget *parent ) const return new QDialogRoundText( parent ); } +void QShapeControllerRoundText::replace( QGraphicsItem *item, const QString &from, const QString &to ) const +{ + QLightRoundTextItem *textItem = static_cast< QLightRoundTextItem * >( item ); + if( textItem->text() == from ) + textItem->setText( to ); +} + void QShapeControllerRoundText::writeData( QXmlStreamWriter &writer, const QGraphicsItem *item ) const { const QLightRoundTextItem *textItem = static_cast< const QLightRoundTextItem * >( item ); diff --git a/src/qlightroundtextitem.h b/src/qlightroundtextitem.h index ac1ac12..69d9688 100644 --- a/src/qlightroundtextitem.h +++ b/src/qlightroundtextitem.h @@ -74,6 +74,7 @@ public: virtual QString menuName() const; virtual QGraphicsItem *create() const; + virtual void replace( QGraphicsItem *item, const QString &from, const QString &to ) const; protected: virtual QItemDialog *createDialog( QWidget *parent ) const; diff --git a/src/qlighttextitem.cpp b/src/qlighttextitem.cpp index 7c1a950..ba07e26 100644 --- a/src/qlighttextitem.cpp +++ b/src/qlighttextitem.cpp @@ -78,6 +78,13 @@ void QShapeControllerText::writeData( QXmlStreamWriter &writer, const QGraphicsI writer.writeTextElement( "text", textItem->text() ); } +void QShapeControllerText::replace( QGraphicsItem *item, const QString &from, const QString &to ) const +{ + QGraphicsSimpleTextItem *textItem = static_cast< QGraphicsSimpleTextItem * >( item ); + if( textItem->text() == from ) + textItem->setText( to ); +} + void QShapeControllerText::readData( const QString &element, const QXmlStreamAttributes &attrs, diff --git a/src/qlighttextitem.h b/src/qlighttextitem.h index c655972..4f84074 100644 --- a/src/qlighttextitem.h +++ b/src/qlighttextitem.h @@ -43,6 +43,7 @@ public: virtual QString menuName() const; virtual QGraphicsItem *create() const; + virtual void replace( QGraphicsItem *item, const QString &from, const QString &to ) const; protected: virtual QItemDialog *createDialog( QWidget *parent ) const; diff --git a/src/qlscribe.h b/src/qlscribe.h index 32b45d7..c419a2e 100644 --- a/src/qlscribe.h +++ b/src/qlscribe.h @@ -16,11 +16,14 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - $Id:$ */ + $Id$ */ #ifndef QLSCRIBE_H #define QLSCRIBE_H +#include +#include + enum LabelMode { /** label the entire disc */ modeFull=0, @@ -30,4 +33,6 @@ enum LabelMode { modeContent=2 }; +std::ostream &operator<<( std::ostream &os, const QString &str ); + #endif // QLSCRIBE_H diff --git a/src/qlscribe.pro b/src/qlscribe.pro index 7ba5e31..96d9b3d 100644 --- a/src/qlscribe.pro +++ b/src/qlscribe.pro @@ -19,7 +19,8 @@ SOURCES += main.cpp \ qdialogprogress.cpp \ qlighttextitem.cpp \ qlightpixmapitem.cpp \ - qdialogcdproperties.cpp + qdialogcdproperties.cpp \ + qconsoleprintprogress.cpp QMAKE_LIBS += -llightscribe HEADERS += mainwindow.h \ qlightroundtextitem.h \ @@ -35,7 +36,8 @@ HEADERS += mainwindow.h \ qlighttextitem.h \ qlightpixmapitem.h \ qlscribe.h \ - qdialogcdproperties.h + qdialogcdproperties.h \ + qconsoleprintprogress.h FORMS += qdialogtext.ui \ qdialogroundtext.ui \ qdialogpixmap.ui \ diff --git a/src/qshapefactory.cpp b/src/qshapefactory.cpp index ee2a51c..81e1344 100644 --- a/src/qshapefactory.cpp +++ b/src/qshapefactory.cpp @@ -88,6 +88,10 @@ QGraphicsItem *QShapeController::read( QXmlStreamReader &reader ) const return item; } +void QShapeController::replace( QGraphicsItem *, const QString &, const QString & ) const +{ +} + QShapeFactory &QShapeFactory::QShapeFactory::instance() { static QShapeFactory inst; diff --git a/src/qshapefactory.h b/src/qshapefactory.h index b8b1f58..aaa900b 100644 --- a/src/qshapefactory.h +++ b/src/qshapefactory.h @@ -16,7 +16,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - $Id:$ */ + $Id$ */ #ifndef QSHAPEFACTORY_H #define QSHAPEFACTORY_H @@ -46,10 +46,13 @@ public: 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; + virtual void replace( QGraphicsItem *item, const QString &from, const QString &to ) const; + protected: virtual QItemDialog *createDialog( QWidget *parent ) const = 0; virtual void writeData( QXmlStreamWriter &writer, const QGraphicsItem *item ) const = 0;