/*************************************************************************** * Copyright (C) 2004 by Johan Maes ON4QZ * * on4qz@telenet.be * * * * http://users.telenet.be/on4qz * * * * 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., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "utils.h" #include bool getValue(int &val, QLineEdit* input) { bool ok; QString s; s=input->text(); val=s.toInt(&ok); return ok; } bool getValue(double &val, QLineEdit* input) { bool ok; QString s; s=input->text(); val=s.toDouble(&ok); return ok; } bool getValue(int &val, QString input) { bool ok; val=input.toInt(&ok); return ok; } bool getValue(double &val, QString input) { bool ok; val=input.toDouble(&ok); return ok; } void getValue(bool &val, QCheckBox *input) { val=input->isChecked(); } void getValue(int &val, QSpinBox *input) { val=input->value(); } void getValue(QString &s, QLineEdit *input) { s=input->text(); } void getValue(int &s, QComboBox *input) { s=input->currentItem(); } void getValue(QString &s, QComboBox *input) { s=input->currentText(); } void getValue(int &s, QButtonGroup *input) { s=input->selectedId(); } void setValue(int val, QLineEdit* output) { output->setText(QString::number(val)); } void setValue(double val, QLineEdit* output) { output->setText(QString::number(val)); } void setValue(bool val, QCheckBox *input) { input->setChecked(val); } void setValue(int val, QSpinBox *input) { input->setValue(val); } void setValue(QString s, QLineEdit *input) { input->setText(s); } void setValue(int s, QComboBox *input) { input->setCurrentItem(s); } void setValue(QString s, QComboBox *input) { int i; for(i=0;icount();i++) { if(input->text(i)==s) { input->setCurrentItem(i); return; } } input->setCurrentItem(0); } void setValue(int s, QButtonGroup *input) { input->setButton(s); } bool browseGetFile(QLineEdit *le,QString deflt, const QString &filter) { dirDialog d; QString s=d.openFileName(deflt,filter); if (s==QString::null) return FALSE; if (s.isEmpty()) return FALSE; le->setText(s); return TRUE; } bool browseSaveFile(QLineEdit *le,QString deflt,const QString &filter) { dirDialog d; QString s=d.saveFileName(deflt,filter,""); if (s==QString::null) return FALSE; if (s.isEmpty()) return FALSE; le->setText(s); return TRUE; } bool browseDir(QLineEdit *le,QString deflt,const QString &filter) { dirDialog d; QString s=d.openDirName(deflt,filter); if (s==QString::null) return FALSE; if (s.isEmpty()) return FALSE; le->setText(s); return TRUE; } PixmapView::PixmapView( QWidget *parent ) : QScrollView( parent ) { viewport()->setBackgroundMode( PaletteBase ); } void PixmapView::setPixmap( const QPixmap &pix ) { pixmap = pix; resizeContents( pixmap.size().width(), pixmap.size().height() ); viewport()->repaint( FALSE ); } void PixmapView::drawContents( QPainter *p, int cx, int cy, int cw, int ch ) { p->fillRect( cx, cy, cw, ch, colorGroup().brush( QColorGroup::Base ) ); p->drawPixmap( 0, 0, pixmap ); } // **************************************************************************************************** preview::preview( QWidget *parent ) : QWidget(parent ) { pixmap = new PixmapView( this ); //raiseWidget( pixmap ); } void preview::resizeEvent(QResizeEvent *p) { debug ("preview resize"); pixmap->resize(p->size().width(),p->size().height()); } void preview::previewUrl( const QUrl &u,int &w,int &h) { w=h=0; if ( u.isLocalFile() ) { QString path = u.path(); QFileInfo fi( path ); QPixmap pix( path ); if (!pix.isNull() ) { pixmap->setPixmap( pix ); w=pix.size().width(); h= pix.size().height(); } } } PreviewWidget::PreviewWidget( QWidget *parent ) : QVBox( parent ), QFilePreview() { setSpacing( 5 ); setMargin( 5 ); QHBox *row = new QHBox( this ); row->setSpacing( 5 ); infoLabel=new QLabel( tr( "File Information" ), row ); row->setFixedHeight( 10 + infoLabel->sizeHint().height() ); Preview = new preview( this ); } void PreviewWidget::previewUrl( const QUrl &u ) { int w,h; QString s; Preview->previewUrl( u,w,h); s.sprintf("W=%d H=%d",w,h); infoLabel->setText(s); } // **************************************************************************************************** dirDialog::dirDialog(QWidget * parent ,const char * name,bool modal) : QFileDialog( parent, name, modal) { setContentsPreviewEnabled( TRUE ); PreviewWidget *pw = new PreviewWidget( this ); setContentsPreview( pw, pw ); setViewMode( QFileDialog::List); setPreviewMode( QFileDialog::Contents ); } dirDialog::~dirDialog() { } QString dirDialog::openFileName(const QString &startWith, const QString &filter, bool single) { QString start=startWith; setPreviewMode( QFileDialog::Contents ); if ( !start ) { start = QDir::currentDirPath(); } if(single) setMode(ExistingFile); else setMode(ExistingFiles); setFilter(filter); setSelection( start); if(exec()==QDialog::Accepted) { return selectedFile(); } return ""; } /*! \fn dirDialog::openDirName(const QString &startWith, const QString &filter) \brief selection of a directory Opens a directory \param startWith directory to open (preselected) \param filter types to select from (e.g. mydirs*) \return if canceled or no selection then return an empty string else return string containing absolute dirname */ QString dirDialog::openDirName(const QString &startWith, const QString &filter) { QString start=startWith; setPreviewMode( QFileDialog::NoPreview ); if ( !start ) { start = QDir::currentDirPath(); } setMode(DirectoryOnly); setFilter(filter); setSelection( start); if(exec()) { return selectedFile(); } return ""; } /*! \fn dirDialog::saveFileName(const QString &startWith, const QString &filter,QString extension) \brief Save a file to disk Saves a file to disk. A dialogbox is opened with \a startWith directory (or /dir/subdir/..../filename) preselected \param startWith directory to open (can include filename to preselect) \param filter file types to select from (e.g. *.txt *.doc) \param extension if extension is not empty or NULL, thenn this string will be appended to the filename. A dot will automatically be insterted (i.e specify "txt" not ".txt"). \return if canceled or no selection then return an empty string else return string containing absolute filename. */ QString dirDialog::saveFileName(const QString &startWith, const QString &filter,QString extension) { QString start(startWith); QString exten(extension); setPreviewMode( QFileDialog::NoPreview ); if ( !start ) { start = QDir::currentDirPath(); } setMode(AnyFile); setFilter(filter); setSelection( start); if(exec()) { QFileInfo fi(selectedFile()); if(!exten.isEmpty()) { if(fi.extension(FALSE)=="") { fi.setFile(fi.absFilePath()+"."+exten); } } return fi.absFilePath(); } return ""; } logFile::logFile() { #ifdef DEBUGQSSTV lf.setName("qsstv_log.txt"); QFileInfo finf(lf); debug ("opening logfile %s",(const char *)finf.absFilePath()); if(!lf.open(IO_WriteOnly)) { debug("logfile failed"); } ts= new QTextStream( &lf ); #endif } logFile::~logFile() { #ifdef DEBUGQSSTV lf.close(); #endif } #ifdef DEBUGQSSTV void logFile::add(const char *fmt, ...) { va_list args; char str[1000]; va_start(args, fmt); vsnprintf(str, 999, fmt, args); va_end(args); *ts << str <