lib Library API Documentation

kovariable.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include "kovariable.h"
00021 #include "kovariable.moc"
00022 #include <koDocumentInfo.h>
00023 #include <kozoomhandler.h>
00024 #include <klocale.h>
00025 #include <kdebug.h>
00026 #include <kglobal.h>
00027 #include <qdom.h>
00028 #include <koDocument.h>
00029 #include <kdialogbase.h>
00030 #include <kconfig.h>
00031 #include <kinstance.h>
00032 #include <kaboutdata.h>
00033 #include <qstringlist.h>
00034 #include <qcombobox.h>
00035 #include <qvaluelist.h>
00036 #include <qradiobutton.h>
00037 #include "timeformatwidget_impl.h"
00038 #include "dateformatwidget_impl.h"
00039 #include "kocommand.h"
00040 #include "kotextobject.h"
00041 
00042 class KoVariableSettings::KoVariableSettingPrivate
00043 {
00044 public:
00045     KoVariableSettingPrivate() 
00046     {
00047         m_lastPrintingDate.setTime_t(0); // Default is 1970-01-01 midnight locale time
00048     }
00049     QDateTime m_lastPrintingDate;
00050     QDateTime m_creationDate;
00051     QDateTime m_modificationDate;
00052 };
00053 
00054 
00055 KoVariableSettings::KoVariableSettings()
00056 {
00057     d = new KoVariableSettingPrivate;
00058     m_startingPageNumber = 1;
00059     m_displayLink = true;
00060     m_displayComment = true;
00061     m_underlineLink = true;
00062     m_displayFieldCode = false;
00063 }
00064 
00065 KoVariableSettings::~KoVariableSettings()
00066 {
00067     delete d;
00068     d = 0;
00069 }
00070 
00071 QDateTime KoVariableSettings::lastPrintingDate() const
00072 {
00073     return d->m_lastPrintingDate;
00074 }
00075 
00076 void KoVariableSettings::setLastPrintingDate( const QDateTime & _date)
00077 {
00078     d->m_lastPrintingDate = _date;
00079 }
00080 
00081 QDateTime KoVariableSettings::creationDate() const
00082 {
00083     return d->m_creationDate;
00084 }
00085 
00086 void KoVariableSettings::setCreationDate( const QDateTime & _date)
00087 {
00088     if ( !d->m_creationDate.isValid() )
00089         d->m_creationDate = _date;
00090 }
00091 
00092 QDateTime KoVariableSettings::modificationDate() const
00093 {
00094     return d->m_modificationDate;
00095 }
00096 
00097 void KoVariableSettings::setModificationDate( const QDateTime & _date)
00098 {
00099     d->m_modificationDate = _date;
00100 }
00101 
00102 
00103 void KoVariableSettings::save( QDomElement &parentElem )
00104 {
00105      QDomElement elem = parentElem.ownerDocument().createElement( "VARIABLESETTINGS" );
00106      parentElem.appendChild( elem );
00107     if(m_startingPageNumber!=1)
00108     {
00109         elem.setAttribute( "startingPageNumber", m_startingPageNumber );
00110     }
00111     elem.setAttribute("displaylink",(int)m_displayLink);
00112     elem.setAttribute("underlinelink",(int)m_underlineLink);
00113     elem.setAttribute("displaycomment",(int)m_displayComment);
00114     elem.setAttribute("displayfieldcode", (int)m_displayFieldCode);
00115 
00116     if ( d->m_lastPrintingDate.isValid())
00117         elem.setAttribute("lastPrintingDate", d->m_lastPrintingDate.toString(Qt::ISODate));
00118 
00119     if ( d->m_creationDate.isValid())
00120         elem.setAttribute("creationDate", d->m_creationDate.toString(Qt::ISODate));
00121 
00122     if ( d->m_modificationDate.isValid())
00123         elem.setAttribute("modificationDate", d->m_modificationDate.toString(Qt::ISODate));
00124 }
00125 
00126 void KoVariableSettings::load( QDomElement &elem )
00127 {
00128     QDomElement e = elem.namedItem( "VARIABLESETTINGS" ).toElement();
00129     if (!e.isNull())
00130     {
00131         if(e.hasAttribute("startingPageNumber"))
00132             m_startingPageNumber = e.attribute("startingPageNumber").toInt();
00133         if(e.hasAttribute("displaylink"))
00134             m_displayLink=(bool)e.attribute("displaylink").toInt();
00135         if(e.hasAttribute("underlinelink"))
00136             m_underlineLink=(bool)e.attribute("underlinelink").toInt();
00137         if(e.hasAttribute("displaycomment"))
00138             m_displayComment=(bool)e.attribute("displaycomment").toInt();
00139         if (e.hasAttribute("displayfieldcode"))
00140             m_displayFieldCode=(bool)e.attribute("displayfieldcode").toInt();
00141 
00142         if (e.hasAttribute("lastPrintingDate"))
00143             d->m_lastPrintingDate = QDateTime::fromString( e.attribute( "lastPrintingDate" ), Qt::ISODate );
00144         else
00145             d->m_lastPrintingDate.setTime_t(0); // 1970-01-01 00:00:00.000 locale time
00146 
00147         if (e.hasAttribute("creationDate"))
00148             d->m_creationDate = QDateTime::fromString( e.attribute( "creationDate" ), Qt::ISODate );
00149 
00150         if (e.hasAttribute("modificationDate"))
00151             d->m_modificationDate = QDateTime::fromString( e.attribute( "modificationDate" ), Qt::ISODate );
00152     }
00153 }
00154 
00155 KoVariableDateFormat::KoVariableDateFormat() : KoVariableFormat()
00156 {
00157 }
00158 
00159 QString KoVariableDateFormat::convert( const QVariant& data ) const
00160 {
00161     if ( data.type() != QVariant::Date && data.type() != QVariant::DateTime )
00162     {
00163         kdWarning(32500)<<" Error in KoVariableDateFormat::convert. Value is a "
00164                       << data.typeName() << "(" << data.type() << ")" << endl;
00165         // dateTime will be invalid, then set to 1970-01-01
00166     }
00167     QDateTime dateTime ( data.toDateTime() );
00168     if ( !dateTime.isValid() )
00169         return i18n("No date set"); // e.g. old KWord documents
00170 
00171     if (m_strFormat.lower() == "locale" || m_strFormat.isEmpty())
00172         return KGlobal::locale()->formatDate( dateTime.date(), false );
00173     else if ( m_strFormat.lower() == "localeshort" )
00174         return KGlobal::locale()->formatDate( dateTime.date(), true );
00175     else if ( m_strFormat.lower() == "localedatetime" )
00176         return KGlobal::locale()->formatDateTime( dateTime, false );
00177     else if ( m_strFormat.lower() == "localedatetimeshort" )
00178         return KGlobal::locale()->formatDateTime( dateTime, true );
00179 
00180     QString tmp ( dateTime.toString(m_strFormat) );
00181     const int month = dateTime.date().month();
00182     tmp.replace("PPPP", KGlobal::locale()->monthNamePossessive(month, false)); //long possessive month name
00183     tmp.replace("PPP",  KGlobal::locale()->monthNamePossessive(month, true));  //short possessive month name
00184     return tmp;
00185 }
00186 
00187 QCString KoVariableDateFormat::key() const
00188 {
00189     return getKey( m_strFormat );
00190 }
00191 
00192 QCString KoVariableDateFormat::getKey( const QString& props ) const
00193 {
00194     return QCString("DATE") + props.utf8();
00195 }
00196 
00197 void KoVariableDateFormat::load( const QCString &key )
00198 {
00199     QCString params( key.mid( 4 ) );
00200     if ( !params.isEmpty() )
00201     {
00202         if (params[0] == '1' || params[0] == '0') // old m_bShort crap
00203             params = params.mid(1); // skip it
00204         m_strFormat = QString::fromUtf8( params ); // skip "DATE"
00205     }
00206 }
00207 
00208 // Used by KoVariableFormatCollection::popupActionList(), to apply all formats
00209 // to the current data, in the popup menu.
00210 QStringList KoVariableDateFormat::staticFormatPropsList()
00211 {
00212     QStringList listDateFormat;
00213     listDateFormat<<"locale";
00214     listDateFormat<<"localeshort";
00215     listDateFormat<<"localedatetime";
00216     listDateFormat<<"localedatetimeshort";
00217     listDateFormat<<"dd/MM/yy";
00218     listDateFormat<<"dd/MM/yyyy";
00219     listDateFormat<<"MMM dd,yy";
00220     listDateFormat<<"MMM dd,yyyy";
00221     listDateFormat<<"dd.MMM.yyyy";
00222     listDateFormat<<"MMMM dd, yyyy";
00223     listDateFormat<<"ddd, MMM dd,yy";
00224     listDateFormat<<"dddd, MMM dd,yy";
00225     listDateFormat<<"MM-dd";
00226     listDateFormat<<"yyyy-MM-dd";
00227     listDateFormat<<"dd/yy";
00228     listDateFormat<<"MMMM";
00229     listDateFormat<<"yyyy-MM-dd hh:mm";
00230     listDateFormat<<"dd.MMM.yyyy hh:mm";
00231     listDateFormat<<"MMM dd,yyyy h:mm AP";
00232     listDateFormat<<"yyyy-MM-ddThh:mm:ss"; // ISO 8601
00233     return listDateFormat;
00234 }
00235 
00236 // Used by dateformatwidget_impl
00237 // TODO: shouldn't it apply the formats to the value, like the popupmenu does?
00238 QStringList KoVariableDateFormat::staticTranslatedFormatPropsList()
00239 {
00240     QStringList listDateFormat;
00241     listDateFormat<<i18n("Locale date format");
00242     listDateFormat<<i18n("Short locale date format");
00243     listDateFormat<<i18n("Locale date & time format");
00244     listDateFormat<<i18n("Short locale date & time format");
00245     listDateFormat<<"dd/MM/yy";
00246     listDateFormat<<"dd/MM/yyyy";
00247     listDateFormat<<"MMM dd,yy";
00248     listDateFormat<<"MMM dd,yyyy";
00249     listDateFormat<<"dd.MMM.yyyy";
00250     listDateFormat<<"MMMM dd, yyyy";
00251     listDateFormat<<"ddd, MMM dd,yy";
00252     listDateFormat<<"dddd, MMM dd,yy";
00253     listDateFormat<<"MM-dd";
00254     listDateFormat<<"yyyy-MM-dd";
00255     listDateFormat<<"dd/yy";
00256     listDateFormat<<"MMMM";
00257     listDateFormat<<"yyyy-MM-dd hh:mm";
00258     listDateFormat<<"dd.MMM.yyyy hh:mm";
00259     listDateFormat<<"MMM dd,yyyy h:mm AP";
00260     listDateFormat<<"yyyy-MM-ddThh:mm:ss"; // ISO 8601
00261     return listDateFormat;
00262 }
00263 
00265 
00266 KoVariableTimeFormat::KoVariableTimeFormat() : KoVariableFormat()
00267 {
00268 }
00269 
00270 void KoVariableTimeFormat::load( const QCString &key )
00271 {
00272     QCString params( key.mid( 4 ) );
00273     if ( !params.isEmpty() )
00274     m_strFormat = QString::fromUtf8(params);
00275 }
00276 
00277 QString KoVariableTimeFormat::convert( const QVariant & time ) const
00278 {
00279     if ( time.type() != QVariant::Time )
00280     {
00281         kdDebug(32500)<<" Error in KoVariableTimeFormat::convert. Value is a "
00282                       << time.typeName() << "(" << time.type() << ")" << endl;
00283         return QString::null;
00284     }
00285 
00286     if( m_strFormat.lower() == "locale" || m_strFormat.isEmpty() )
00287     return KGlobal::locale()->formatTime( time.toTime() );
00288     return time.toTime().toString(m_strFormat);
00289 }
00290 
00291 QCString KoVariableTimeFormat::key() const
00292 {
00293     return getKey( m_strFormat );
00294 }
00295 
00296 QCString KoVariableTimeFormat::getKey( const QString& props ) const
00297 {
00298     return QCString("TIME") + props.utf8();
00299 }
00300 
00301 // Used by KoVariableFormatCollection::popupActionList(), to apply all formats
00302 // to the current data, in the popup menu.
00303 QStringList KoVariableTimeFormat::staticFormatPropsList()
00304 {
00305     QStringList listTimeFormat;
00306     listTimeFormat<<"locale";
00307     listTimeFormat<<"hh:mm";
00308     listTimeFormat<<"hh:mm:ss";
00309     listTimeFormat<<"hh:mm AP";
00310     listTimeFormat<<"hh:mm:ss AP";
00311     listTimeFormat<<"mm:ss.zzz";
00312     return listTimeFormat;
00313 }
00314 
00315 // Used by timeformatwidget_impl
00316 QStringList KoVariableTimeFormat::staticTranslatedFormatPropsList()
00317 {
00318     QStringList listTimeFormat;
00319     listTimeFormat<<i18n("Locale format");
00320     listTimeFormat<<"hh:mm";
00321     listTimeFormat<<"hh:mm:ss";
00322     listTimeFormat<<"hh:mm AP";
00323     listTimeFormat<<"hh:mm:ss AP";
00324     listTimeFormat<<"mm:ss.zzz";
00325     return listTimeFormat;
00326 }
00327 
00329 
00330 QString KoVariableStringFormat::convert( const QVariant & string ) const
00331 {
00332     if ( string.type() != QVariant::String )
00333     {
00334         kdDebug(32500)<<" Error in KoVariableStringFormat::convert. Value is a " << string.typeName() << endl;
00335         return QString::null;
00336     }
00337 
00338     return string.toString();
00339 }
00340 
00341 QCString KoVariableStringFormat::key() const
00342 {
00343     return getKey( QString::null );
00344     // TODO prefix & suffix
00345 }
00346 
00347 QCString KoVariableStringFormat::getKey( const QString& props ) const
00348 {
00349     return QCString("STRING") + props.utf8();
00350 }
00351 
00353 
00354 QString KoVariableNumberFormat::convert( const QVariant &value ) const
00355 {
00356     if ( value.type() != QVariant::Int )
00357     {
00358         kdDebug(32500)<<" Error in KoVariableNumberFormat::convert. Value is a " << value.typeName() << endl;
00359         return QString::null;
00360     }
00361 
00362     return QString::number( value.toInt() );
00363 }
00364 
00365 QCString KoVariableNumberFormat::key() const
00366 {
00367     return getKey(QString::null);
00368 }
00369 
00370 QCString KoVariableNumberFormat::getKey( const QString& props ) const
00371 {
00372     return QCString("NUMB") + props.utf8();
00373 }
00374 
00376 
00377 KoVariableFormatCollection::KoVariableFormatCollection()
00378 {
00379     m_dict.setAutoDelete( true );
00380 }
00381 
00382 KoVariableFormat * KoVariableFormatCollection::format( const QCString &key )
00383 {
00384     KoVariableFormat *f = m_dict[ key.data() ];
00385     if (f)
00386         return f;
00387     else
00388         return createFormat( key );
00389 }
00390 
00391 KoVariableFormat * KoVariableFormatCollection::createFormat( const QCString &key )
00392 {
00393     kdDebug(32500) << "KoVariableFormatCollection: creating format for key=" << key << endl;
00394     KoVariableFormat * format = 0L;
00395     // The first 4 chars identify the class
00396     QCString type = key.left(4);
00397     if ( type == "DATE" )
00398         format = new KoVariableDateFormat();
00399     else if ( type == "TIME" )
00400         format = new KoVariableTimeFormat();
00401     else if ( type == "NUMB" ) // this type of programming makes me numb ;)
00402         format = new KoVariableNumberFormat();
00403     else if ( type == "STRI" )
00404         format = new KoVariableStringFormat();
00405 
00406     if ( format )
00407     {
00408         format->load( key );
00409         m_dict.insert( format->key() /* not 'key', it could be incomplete */, format );
00410     }
00411     return format;
00412 }
00413 
00414 /******************************************************************/
00415 /* Class:       KoVariableCollection                              */
00416 /******************************************************************/
00417 KoVariableCollection::KoVariableCollection(KoVariableSettings *_settings, KoVariableFormatCollection *formatCollection)
00418 {
00419     m_variableSettings = _settings;
00420     m_varSelected = 0L;
00421     m_formatCollection = formatCollection;
00422 }
00423 
00424 KoVariableCollection::~KoVariableCollection()
00425 {
00426     delete m_variableSettings;
00427 }
00428 
00429 void KoVariableCollection::registerVariable( KoVariable *var )
00430 {
00431     if ( !var )
00432         return;
00433     variables.append( var );
00434 }
00435 
00436 void KoVariableCollection::unregisterVariable( KoVariable *var )
00437 {
00438     variables.take( variables.findRef( var ) );
00439 }
00440 
00441 void KoVariableCollection::recalcVariables(int type)
00442 {
00443     bool update = false;
00444     QPtrListIterator<KoVariable> it( variables );
00445     for ( ; it.current() ; ++it )
00446     {
00447         if ( it.current()->isDeleted() )
00448             continue;
00449         if ( it.current()->type() == type || type == VT_ALL )
00450         {
00451             update = true;
00452             it.current()->recalc();
00453             KoTextParag * parag = it.current()->paragraph();
00454             if ( parag )
00455             {
00456                 //kdDebug(32500) << "KoDoc::recalcVariables -> invalidating parag " << parag->paragId() << endl;
00457                 parag->invalidate( 0 );
00458                 parag->setChanged( true );
00459             }
00460         }
00461     }
00462     // TODO pass list of textdocuments as argument
00463     // Or even better, call emitRepaintChanged on all modified textobjects
00464     if(update)
00465         emit repaintVariable();
00466 }
00467 
00468 
00469 void KoVariableCollection::setVariableValue( const QString &name, const QString &value )
00470 {
00471     varValues[ name ] = value;
00472 }
00473 
00474 QString KoVariableCollection::getVariableValue( const QString &name ) const
00475 {
00476     if ( !varValues.contains( name ) )
00477         return i18n( "No value" );
00478     return varValues[ name ];
00479 }
00480 
00481 bool KoVariableCollection::customVariableExist(const QString &varname) const
00482 {
00483     return varValues.contains( varname );
00484 }
00485 
00486 void KoVariableCollection::recalcVariables(KoVariable *var)
00487 {
00488     if( var )
00489     {
00490         var->recalc();
00491         KoTextParag * parag = var->paragraph();
00492         if ( parag )
00493         {
00494             parag->invalidate( 0 );
00495             parag->setChanged( true );
00496         }
00497         emit repaintVariable();
00498     }
00499 }
00500 
00501 void KoVariableCollection::setVariableSelected(KoVariable * var)
00502 {
00503     m_varSelected=var;
00504 }
00505 
00506 QPtrList<KAction> KoVariableCollection::popupActionList()
00507 {
00508     QPtrList<KAction> listAction;
00509     // Insert list of actions that change the subtype
00510     QStringList list = m_varSelected->subTypeText();
00511     QStringList::ConstIterator it = list.begin();
00512     for ( int i = 0; it != list.end() ; ++it, ++i )
00513     {
00514         if ( !(*it).isEmpty() ) // in case of removed subtypes or placeholders
00515         {
00516             // We store the subtype number as the action name
00517             QCString name; name.setNum(i);
00518             KToggleAction * act = new KToggleAction( *it, KShortcut(), 0, name );
00519             connect( act, SIGNAL(activated()), this, SLOT(slotChangeSubType()) );
00520             if ( i == m_varSelected->subType() )
00521                 act->setChecked( true );
00522             //m_subTextMap.insert( act, i );
00523             listAction.append( act );
00524         }
00525     }
00526     // Insert list of actions that change the format properties
00527     KoVariableFormat* format = m_varSelected->variableFormat();
00528     QString currentFormat = format->formatProperties();
00529 
00530     list = format->formatPropsList();
00531     it = list.begin();
00532     for ( int i = 0; it != list.end() ; ++it, ++i )
00533     {
00534         if( i == 0 ) // first item, and list not empty
00535             listAction.append( new KActionSeparator() );
00536 
00537         if ( !(*it).isEmpty() ) // in case of removed subtypes or placeholders
00538         {
00539             format->setFormatProperties( *it ); // temporary change
00540             QString text = format->convert( m_varSelected->varValue() );
00541             // We store the raw format as the action name
00542             KToggleAction * act = new KToggleAction(text, KShortcut(), 0, (*it).utf8());
00543             connect( act, SIGNAL(activated()), this, SLOT(slotChangeFormat()) );
00544             if ( (*it) == currentFormat )
00545                 act->setChecked( true );
00546             listAction.append( act );
00547         }
00548     }
00549 
00550     // Restore current format
00551     format->setFormatProperties( currentFormat );
00552     return listAction;
00553 }
00554 
00555 void KoVariableCollection::slotChangeSubType()
00556 {
00557     KAction * act = (KAction *)(sender());
00558     int menuNumber = QCString(act->name()).toInt();
00559     int newSubType = m_varSelected->variableSubType(menuNumber);
00560     kdDebug(32500) << "slotChangeSubType: menuNumber=" << menuNumber << " newSubType=" << newSubType << endl;
00561     if ( m_varSelected->subType() != newSubType )
00562     {
00563         KoChangeVariableSubType *cmd=new KoChangeVariableSubType(
00564             m_varSelected->subType(), newSubType, m_varSelected );
00565         cmd->execute();
00566         m_varSelected->textDocument()->emitNewCommand(cmd);
00567     }
00568 }
00569 
00570 void KoVariableCollection::slotChangeFormat()
00571 {
00572     KAction * act = (KAction *)(sender());
00573     QString newFormat = QString::fromUtf8(act->name());
00574     QString oldFormat = m_varSelected->variableFormat()->formatProperties();
00575     if (oldFormat != newFormat )
00576     {
00577         KCommand *cmd=new KoChangeVariableFormatProperties(
00578             oldFormat, newFormat, m_varSelected );
00579         cmd->execute();
00580         m_varSelected->textDocument()->emitNewCommand(cmd);
00581     }
00582 }
00583 
00584 /******************************************************************/
00585 /* Class: KoVariable                                              */
00586 /******************************************************************/
00587 KoVariable::KoVariable( KoTextDocument *textdoc, KoVariableFormat *varFormat, KoVariableCollection *_varColl)
00588     : KoTextCustomItem( textdoc )
00589 {
00590     //d = new Private;
00591     m_varColl=_varColl;
00592     m_varFormat = varFormat;
00593     m_varColl->registerVariable( this );
00594     m_ascent = 0;
00595 }
00596 
00597 KoVariable::~KoVariable()
00598 {
00599     //kdDebug(32500) << "KoVariable::~KoVariable " << this << endl;
00600     m_varColl->unregisterVariable( this );
00601     //delete d;
00602 }
00603 
00604 QStringList KoVariable::subTypeText()
00605 {
00606     return QStringList();
00607 }
00608 
00609 void KoVariable::resize()
00610 {
00611     if ( m_deleted )
00612         return;
00613     KoTextFormat *fmt = format();
00614     QFontMetrics fm = fmt->refFontMetrics();
00615     QString txt = text();
00616 
00617     width = 0;
00618     for ( int i = 0 ; i < (int)txt.length() ; ++i )
00619         width += fm.charWidth( txt, i ); // size at 100%
00620     // zoom to LU
00621     width = qRound( KoTextZoomHandler::ptToLayoutUnitPt( width ) );
00622     height = fmt->height();
00623     m_ascent = fmt->ascent();
00624     //kdDebug(32500) << "KoVariable::resize text=" << txt << " width=" << width << " height=" << height << " ascent=" << m_ascent << endl;
00625 }
00626 
00627 void KoVariable::recalcAndRepaint()
00628 {
00629     recalc();
00630     KoTextParag * parag = paragraph();
00631     if ( parag )
00632     {
00633         //kdDebug(32500) << "KoVariable::recalcAndRepaint -> invalidating parag " << parag->paragId() << endl;
00634         parag->invalidate( 0 );
00635         parag->setChanged( true );
00636     }
00637     textDocument()->emitRepaintChanged();
00638 }
00639 
00640 QString KoVariable::fieldCode()
00641 {
00642     return i18n("Variable");
00643 }
00644 
00645 QString KoVariable::text(bool realValue)
00646 {
00647     KoTextFormat *fmt = format();
00648     QString str;
00649     if (m_varColl->variableSetting()->displayFieldCode()&&!realValue)
00650         str = fieldCode();
00651     else
00652         str = m_varFormat->convert( m_varValue );
00653 
00654     return fmt->displayedString( str);
00655 }
00656 
00657 void KoVariable::drawCustomItem( QPainter* p, int x, int y, int wpix, int hpix, int ascentpix, int /*cx*/, int /*cy*/, int /*cw*/, int /*ch*/, const QColorGroup& cg, bool selected, int offset, bool drawingShadow )
00658 {
00659     KoTextFormat * fmt = format();
00660     KoZoomHandler * zh = textDocument()->paintingZoomHandler();
00661     QFont font( fmt->screenFont( zh ) );
00662     drawCustomItemHelper( p, x, y, wpix, hpix, ascentpix, cg, selected, offset, fmt, font, fmt->color(), drawingShadow );
00663 }
00664 
00665 void KoVariable::drawCustomItemHelper( QPainter* p, int x, int y, int wpix, int hpix, int ascentpix, const QColorGroup& cg, bool selected, int offset, KoTextFormat* fmt, const QFont& font, QColor textColor, bool drawingShadow )
00666 {
00667     // Important: the y value already includes the difference between the parag baseline
00668     // and the char's own baseline (ascent) (see paintDefault in korichtext.cpp)
00669     // So we just draw the text there. But we need the baseline for drawFontEffects...
00670     KoZoomHandler * zh = textDocument()->paintingZoomHandler();
00671 
00672     p->save();
00673 
00674     if ( fmt->textBackgroundColor().isValid() )
00675         p->fillRect( x, y, wpix, hpix, fmt->textBackgroundColor() );
00676 
00677     if ( drawingShadow ) // Use shadow color if drawing a shadow
00678     {
00679         textColor = fmt->shadowColor();
00680         p->setPen( textColor );
00681     }
00682     else if ( selected )
00683     {
00684         textColor = cg.color( QColorGroup::HighlightedText );
00685         p->setPen( QPen( textColor ) );
00686         p->fillRect( x, y, wpix, hpix, cg.color( QColorGroup::Highlight ) );
00687     }
00688     else if ( textDocument() && textDocument()->drawFormattingChars()
00689               && p->device()->devType() != QInternal::Printer )
00690     {
00691         textColor = cg.color( QColorGroup::Highlight );
00692         p->setPen( QPen ( textColor, 0, Qt::DotLine ) );
00693         p->drawRect( x, y, wpix, hpix );
00694     }
00695     else {
00696         if ( !textColor.isValid() ) // Resolve the color at this point
00697             textColor = KoTextFormat::defaultTextColor( p );
00698         p->setPen( QPen( textColor ) );
00699     }
00700 
00701     p->setFont( font ); // already done by KoTextCustomItem::draw but someone might
00702                         // change the font passed to drawCustomItemHelper (e.g. KoLinkVariable)
00703     QString str = text();
00704     KoTextParag::drawFontEffects( p, fmt, zh, font, textColor, x, ascentpix, wpix, y, hpix, str[0] );
00705     int posY = y + ascentpix + offset;
00706     if ( fmt->vAlign() == KoTextFormat::AlignSubScript )
00707         posY +=p->fontMetrics().height() / 6;
00708     if ( fmt->vAlign() != KoTextFormat::AlignSuperScript )
00709         posY -= fmt->offsetFromBaseLine();
00710     else if ( fmt->offsetFromBaseLine() < 0 )
00711         posY -= 2*fmt->offsetFromBaseLine();
00712 
00713     p->drawText( x, posY, str );
00714     p->restore();
00715 }
00716 
00717 void KoVariable::save( QDomElement &parentElem )
00718 {
00719     //kdDebug(32500) << "KoVariable::save" << endl;
00720     QDomElement variableElem = parentElem.ownerDocument().createElement( "VARIABLE" );
00721     parentElem.appendChild( variableElem );
00722     QDomElement typeElem = parentElem.ownerDocument().createElement( "TYPE" );
00723     variableElem.appendChild( typeElem );
00724     typeElem.setAttribute( "type", static_cast<int>( type() ) );
00726     typeElem.setAttribute( "key", m_varFormat->key() );
00727     typeElem.setAttribute( "text", text(true) );
00728     if ( correctValue() != 0)
00729         typeElem.setAttribute( "correct", correctValue() );
00730     saveVariable( variableElem );
00731 }
00732 
00733 void KoVariable::load( QDomElement & )
00734 {
00735 }
00736 
00737 KoVariable * KoVariableCollection::createVariable( int type, short int subtype, KoVariableFormatCollection * coll, KoVariableFormat *varFormat,KoTextDocument *textdoc, KoDocument * doc, int _correct, bool _forceDefaultFormat )
00738 {
00739     QCString string;
00740     QStringList stringList;
00741     if ( varFormat == 0L )
00742     {
00743         // Get the default format for this variable (this method is only called in the interactive case, not when loading)
00744         switch ( type ) {
00745         case VT_DATE:
00746         case VT_DATE_VAR_KWORD10:  // compatibility with kword 1.0
00747         {
00748             if ( _forceDefaultFormat )
00749                 varFormat = coll->format( KoDateVariable::defaultFormat() );
00750             else
00751             {
00752                 QCString result = KoDateVariable::formatStr(_correct);
00753                 if ( result == 0 )//we cancel insert variable
00754                     return 0L;
00755                 varFormat = coll->format( result );
00756             }
00757             break;
00758         }
00759         case VT_TIME:
00760         case VT_TIME_VAR_KWORD10:  // compatibility with kword 1.0
00761         {
00762             if ( _forceDefaultFormat )
00763                 varFormat = coll->format( KoTimeVariable::defaultFormat() );
00764             else
00765                 varFormat = coll->format( KoTimeVariable::formatStr(_correct) );
00766             break;
00767         }
00768         case VT_PGNUM:
00769             varFormat = coll->format( "NUMBER" );
00770             break;
00771         case VT_FIELD:
00772         case VT_CUSTOM:
00773         case VT_MAILMERGE:
00774         case VT_LINK:
00775         case VT_NOTE:
00776             varFormat = coll->format( "STRING" );
00777             break;
00778         case VT_FOOTNOTE: // this is a KWord-specific variable
00779             kdError() << "Footnote type not handled in KoVariableCollection: VT_FOOTNOTE" << endl;
00780             return 0L;
00781         }
00782     }
00783     Q_ASSERT( varFormat );
00784     if ( varFormat == 0L ) // still 0 ? Impossible!
00785         return 0L ;
00786 
00787     kdDebug(32500) << "Creating variable. Format=" << varFormat->key() << " type=" << type << endl;
00788     KoVariable * var = 0L;
00789     switch ( type ) {
00790         case VT_DATE:
00791         case VT_DATE_VAR_KWORD10:  // compatibility with kword 1.0
00792             var = new KoDateVariable( textdoc, subtype, varFormat, this, _correct );
00793             break;
00794         case VT_TIME:
00795         case VT_TIME_VAR_KWORD10:  // compatibility with kword 1.0
00796             var = new KoTimeVariable( textdoc, subtype, varFormat, this, _correct );
00797             break;
00798         case VT_PGNUM:
00799             kdError() << "VT_PGNUM must be handled by the application's reimplementation of KoVariableCollection::createVariable" << endl;
00800             //var = new KoPgNumVariable( textdoc, subtype, varFormat, this );
00801             break;
00802         case VT_FIELD:
00803             var = new KoFieldVariable( textdoc, subtype, varFormat,this,doc );
00804             break;
00805         case VT_CUSTOM:
00806             var = new KoCustomVariable( textdoc, QString::null, varFormat, this);
00807             break;
00808         case VT_MAILMERGE:
00809             var = new KoMailMergeVariable( textdoc, QString::null, varFormat ,this);
00810             break;
00811         case VT_LINK:
00812             var = new KoLinkVariable( textdoc,QString::null, QString::null, varFormat ,this);
00813             break;
00814         case VT_NOTE:
00815             var = new KoNoteVariable( textdoc, QString::null, varFormat ,this);
00816             break;
00817     }
00818     Q_ASSERT( var );
00819     return var;
00820 }
00821 
00822 void KoVariable::setVariableFormat( KoVariableFormat *_varFormat )
00823 {
00824     // TODO if ( _varFormat ) _varFormat->deref();
00825     m_varFormat = _varFormat;
00826     // TODO m_varFormat->ref();
00827 }
00828 
00829 /******************************************************************/
00830 /* Class: KoDateVariable                                          */
00831 /******************************************************************/
00832 KoDateVariable::KoDateVariable( KoTextDocument *textdoc, short int subtype, KoVariableFormat *_varFormat, KoVariableCollection *_varColl, int _correctDate)
00833     : KoVariable( textdoc, _varFormat,_varColl ), m_subtype( subtype ), m_correctDate( _correctDate)
00834 {
00835 }
00836 
00837 QString KoDateVariable::fieldCode()
00838 {
00839     if ( m_subtype == VST_DATE_FIX )
00840         return i18n("Date (Fixed)");
00841     else if ( m_subtype == VST_DATE_CURRENT)
00842         return i18n("Date");
00843     else if ( m_subtype == VST_DATE_LAST_PRINTING)
00844         return i18n("Last Printing");
00845     else if ( m_subtype == VST_DATE_CREATE_FILE )
00846         return i18n( "File Creation");
00847     else if ( m_subtype == VST_DATE_MODIFY_FILE )
00848         return i18n( "File Modification");
00849     else
00850         return i18n("Date");
00851 }
00852 
00853 void KoDateVariable::resize()
00854 {
00855     KoTextFormat * fmt = format();
00856     QString oldLanguage;
00857     if ( !fmt->language().isEmpty())
00858     {
00859          oldLanguage=KGlobal::locale()->language();
00860          bool changeLanguage = KGlobal::locale()->setLanguage( fmt->language() );
00861          KoVariable::resize();
00862          if ( changeLanguage )
00863              KGlobal::locale()->setLanguage( oldLanguage );
00864     }
00865     else
00866         KoVariable::resize();
00867 }
00868 
00869 void KoDateVariable::recalc()
00870 {
00871     if ( m_subtype == VST_DATE_CURRENT )
00872         m_varValue = QDateTime::currentDateTime().addDays(m_correctDate);
00873     else if ( m_subtype == VST_DATE_LAST_PRINTING )
00874         m_varValue = m_varColl->variableSetting()->lastPrintingDate();
00875     else if ( m_subtype == VST_DATE_CREATE_FILE )
00876         m_varValue = m_varColl->variableSetting()->creationDate();
00877     else if ( m_subtype == VST_DATE_MODIFY_FILE )
00878         m_varValue = m_varColl->variableSetting()->modificationDate();
00879     else
00880     {
00881         // Only if never set before (i.e. upon insertion)
00882         if ( m_varValue.isNull() )
00883             m_varValue = QDateTime::currentDateTime().addDays(m_correctDate);
00884     }
00885     resize();
00886 }
00887 
00888 void KoDateVariable::saveVariable( QDomElement& varElem )
00889 {
00890     QDomElement elem = varElem.ownerDocument().createElement( "DATE" );
00891     varElem.appendChild( elem );
00892     QDate date = m_varValue.toDate(); // works with Date and DateTime
00893     date = date.addDays( -m_correctDate );//remove correctDate value otherwise value stored is bad
00894     elem.setAttribute( "year", date.year() );
00895     elem.setAttribute( "month", date.month() );
00896     elem.setAttribute( "day", date.day() );
00897     elem.setAttribute( "fix", m_subtype == VST_DATE_FIX ); // for compat
00898     elem.setAttribute( "correct", m_correctDate);
00899     elem.setAttribute( "subtype", m_subtype);
00900     if ( m_varValue.type() == QVariant::DateTime )
00901     {
00902         QTime time = m_varValue.toTime();
00903         elem.setAttribute( "hour", time.hour() );
00904         elem.setAttribute( "minute", time.minute() );
00905         elem.setAttribute( "second", time.second() );
00906     }
00907 }
00908 
00909 void KoDateVariable::load( QDomElement& elem )
00910 {
00911     KoVariable::load( elem );
00912 
00913     QDomElement e = elem.namedItem( "DATE" ).toElement();
00914     if (!e.isNull())
00915     {
00916         const int y = e.attribute("year").toInt();
00917         const int month = e.attribute("month").toInt();
00918         const int d = e.attribute("day").toInt();
00919         const int h = e.attribute("hour").toInt();
00920         const int min = e.attribute("minute").toInt();
00921         const int s = e.attribute("second").toInt();
00922         const int ms = e.attribute("msecond").toInt();
00923         const bool fix = e.attribute("fix").toInt() == 1;
00924         if ( e.hasAttribute("correct"))
00925             m_correctDate = e.attribute("correct").toInt();
00926         if ( fix )
00927         {
00928             QDate date( y, month, d );
00929             date = date.addDays( m_correctDate );
00930             const QTime time( h, min, s, ms );
00931             if (time.isValid())
00932                 m_varValue = QVariant ( QDateTime( date, time ) );
00933             else
00934                 m_varValue = QVariant( date );
00935         }
00936         //old date variable format
00937         m_subtype = fix ? VST_DATE_FIX : VST_DATE_CURRENT;
00938         if ( e.hasAttribute( "subtype" ))
00939             m_subtype = e.attribute( "subtype").toInt();
00940     }
00941 }
00942 
00943 QStringList KoDateVariable::actionTexts()
00944 {
00945     QStringList lst;
00946     lst << i18n( "Current Date (fixed)" );
00947     lst << i18n( "Current Date (variable)" );
00948     lst << i18n( "Date of Last Printing" );
00949     lst << i18n( "Date of File Creation" );
00950     lst << i18n( "Date of File Modification" );
00951     return lst;
00952 }
00953 
00954 QStringList KoDateVariable::subTypeText()
00955 {
00956     return KoDateVariable::actionTexts();
00957 }
00958 
00959 QCString KoDateVariable::defaultFormat()
00960 {
00961     return QCString("DATE") + "locale";
00962 }
00963 
00964 QCString KoDateVariable::formatStr(int & correct)
00965 {
00966     QCString string;
00967     QStringList stringList;
00968     KDialogBase* dialog=new KDialogBase(0, 0, true, i18n("Date Format"), KDialogBase::Ok|KDialogBase::Cancel);
00969     DateFormatWidget* widget=new DateFormatWidget(dialog);
00970     int count=0;
00971     dialog->setMainWidget(widget);
00972     KConfig* config = KoGlobal::kofficeConfig();
00973     if( config->hasGroup("Date format history") )
00974     {
00975         KConfigGroupSaver cgs( config, "Date format history");
00976         const int noe=config->readNumEntry("Number Of Entries", 5);
00977         for(int i=0;i<noe;i++)
00978         {
00979             QString num;
00980             num.setNum(i);
00981             const QString tmpString(config->readEntry("Last Used"+num));
00982             if(tmpString.startsWith("locale"))
00983                 continue;
00984             else if(stringList.contains(tmpString))
00985                 continue;
00986             else if(!tmpString.isEmpty())
00987             {
00988                 stringList.append(tmpString);
00989                 count++;
00990             }
00991         }
00992 
00993     }
00994     if(!stringList.isEmpty())
00995     {
00996         widget->combo1->insertItem("---");
00997         widget->combo1->insertStringList(stringList);
00998     }
00999     if(false) { // ### TODO: select the last used item
01000         QComboBox *combo= widget->combo1;
01001         combo->setCurrentItem(combo->count() -1);
01002         widget->updateLabel();
01003     }
01004 
01005     if(dialog->exec()==QDialog::Accepted)
01006     {
01007         string = widget->resultString().utf8();
01008         correct = widget->correctValue();
01009     }
01010     else
01011     {
01012         delete dialog;
01013         return 0;
01014     }
01015     config->setGroup("Date format history");
01016     stringList.remove(string);
01017     stringList.prepend(string);
01018     for(int i=0;i<=count;i++)
01019     {
01020         QString num;
01021         num.setNum(i);
01022         config->writeEntry("Last Used"+num, stringList[i]);
01023     }
01024     config->sync();
01025     delete dialog;
01026     return QCString(QCString("DATE") + string );
01027 }
01028 
01029 /******************************************************************/
01030 /* Class: KoTimeVariable                                          */
01031 /******************************************************************/
01032 KoTimeVariable::KoTimeVariable( KoTextDocument *textdoc, short int subtype, KoVariableFormat *varFormat, KoVariableCollection *_varColl, int _correct)
01033     : KoVariable( textdoc, varFormat,_varColl ), m_subtype( subtype ), m_correctTime( _correct)
01034 {
01035 }
01036 
01037 QString KoTimeVariable::fieldCode()
01038 {
01039     return (m_subtype == VST_TIME_FIX)?i18n("Time (Fixed)"):i18n("Time");
01040 }
01041 
01042 
01043 void KoTimeVariable::resize()
01044 {
01045     KoTextFormat * fmt = format();
01046     if ( !fmt->language().isEmpty() )
01047     {
01048         QString oldLanguage = KGlobal::locale()->language();
01049         bool changeLanguage = KGlobal::locale()->setLanguage( fmt->language() );
01050         KoVariable::resize();
01051         if ( changeLanguage )
01052             KGlobal::locale()->setLanguage( oldLanguage );
01053     }
01054     else
01055         KoVariable::resize();
01056 }
01057 
01058 void KoTimeVariable::recalc()
01059 {
01060     if ( m_subtype == VST_TIME_CURRENT )
01061         m_varValue = QVariant( QTime::currentTime().addSecs(60*m_correctTime));
01062     else
01063     {
01064         // Only if never set before (i.e. upon insertion)
01065         if ( m_varValue.toTime().isNull() )
01066             m_varValue = QVariant( QTime::currentTime().addSecs(60*m_correctTime));
01067     }
01068     resize();
01069 }
01070 
01071 
01072 void KoTimeVariable::saveVariable( QDomElement& parentElem )
01073 {
01074     QDomElement elem = parentElem.ownerDocument().createElement( "TIME" );
01075     parentElem.appendChild( elem );
01076     QTime time = m_varValue.toTime();
01077     time = time.addSecs(-60*m_correctTime);
01078     elem.setAttribute( "hour", time.hour() );
01079     elem.setAttribute( "minute", time.minute() );
01080     elem.setAttribute( "second", time.second() );
01081     elem.setAttribute( "msecond", time.msec() );
01082     elem.setAttribute( "fix", m_subtype == VST_TIME_FIX );
01083     elem.setAttribute( "correct", m_correctTime );
01084 }
01085 
01086 void KoTimeVariable::load( QDomElement& elem )
01087 {
01088     KoVariable::load( elem );
01089 
01090     QDomElement e = elem.namedItem( "TIME" ).toElement();
01091     if (!e.isNull())
01092     {
01093         int h = e.attribute("hour").toInt();
01094         int m = e.attribute("minute").toInt();
01095         int s = e.attribute("second").toInt();
01096         int ms = e.attribute("msecond").toInt();
01097         int correct = 0;
01098         if ( e.hasAttribute("correct"))
01099             correct=e.attribute("correct").toInt();
01100         bool fix = static_cast<bool>( e.attribute("fix").toInt() );
01101         if ( fix )
01102         {
01103             QTime time;
01104             time.setHMS( h, m, s, ms );
01105             time = time.addSecs( 60*m_correctTime );
01106             m_varValue = QVariant( time);
01107 
01108         }
01109         m_subtype = fix ? VST_TIME_FIX : VST_TIME_CURRENT;
01110         m_correctTime = correct;
01111     }
01112 }
01113 
01114 QStringList KoTimeVariable::actionTexts()
01115 {
01116     QStringList lst;
01117     lst << i18n( "Current Time (fixed)" );
01118     lst << i18n( "Current Time (variable)" );
01119     return lst;
01120 }
01121 
01122 QStringList KoTimeVariable::subTypeText()
01123 {
01124     return KoTimeVariable::actionTexts();
01125 }
01126 
01127 QCString KoTimeVariable::formatStr(int & _correct)
01128 {
01129     QCString string;
01130     QStringList stringList;
01131     KDialogBase* dialog=new KDialogBase(0, 0, true, i18n("Time Format"), KDialogBase::Ok|KDialogBase::Cancel);
01132     TimeFormatWidget* widget=new TimeFormatWidget(dialog);
01133     dialog->setMainWidget(widget);
01134     KConfig* config = KoGlobal::kofficeConfig();
01135     int count=0;
01136     if( config->hasGroup("Time format history") )
01137     {
01138         KConfigGroupSaver cgs( config, "Time format history" );
01139         const int noe=config->readNumEntry("Number Of Entries", 5);
01140         for(int i=0;i<noe;i++)
01141         {
01142             QString num;
01143             num.setNum(i);
01144             QString tmpString(config->readEntry("Last Used"+num));
01145             if(tmpString.startsWith("locale"))
01146                 continue;
01147             else if(stringList.contains(tmpString))
01148                 continue;
01149             else if(!tmpString.isEmpty())
01150             {
01151                 stringList.append(tmpString);
01152                 count++;
01153             }
01154         }
01155     }
01156     if(!stringList.isEmpty())
01157     {
01158         widget->combo1->insertItem("---");
01159         widget->combo1->insertStringList(stringList);
01160     }
01161     if(false) // ### TODO: select the last used item
01162     {
01163         QComboBox *combo= widget->combo1;
01164         combo->setCurrentItem(combo->count() -1);
01165     }
01166     if(dialog->exec()==QDialog::Accepted)
01167     {
01168         string = widget->resultString().utf8();
01169         _correct = widget->correctValue();
01170     }
01171     else
01172     {
01173         delete dialog;
01174         return 0;
01175     }
01176     config->setGroup("Time format history");
01177     stringList.remove(string);
01178     stringList.prepend(string);
01179     for(int i=0;i<=count;i++)
01180     {
01181         QString num;
01182         num.setNum(i);
01183         config->writeEntry("Last Used"+num, stringList[i]);
01184     }
01185     config->sync();
01186     delete dialog;
01187     return QCString("TIME"+string );
01188 }
01189 
01190 QCString KoTimeVariable::defaultFormat()
01191 {
01192     return QCString(QCString("TIME")+QCString("locale") );
01193 }
01194 
01195 
01196 /******************************************************************/
01197 /* Class: KoCustomVariable                                        */
01198 /******************************************************************/
01199 KoCustomVariable::KoCustomVariable( KoTextDocument *textdoc, const QString &name, KoVariableFormat *varFormat, KoVariableCollection *_varColl )
01200     : KoVariable( textdoc, varFormat,_varColl )
01201 {
01202     m_varValue = QVariant( name );
01203 }
01204 
01205 QString KoCustomVariable::fieldCode()
01206 {
01207     return i18n("Custom Variable");
01208 }
01209 
01210 QString KoCustomVariable::text(bool realValue)
01211 {
01212     if (m_varColl->variableSetting()->displayFieldCode()&&!realValue)
01213         return fieldCode();
01214     else
01215         return value();
01216 } // use a format when they are customizable
01217 
01218 
01219 
01220 void KoCustomVariable::saveVariable( QDomElement& parentElem )
01221 {
01222     QDomElement elem = parentElem.ownerDocument().createElement( "CUSTOM" );
01223     parentElem.appendChild( elem );
01224     elem.setAttribute( "name", m_varValue.toString() );
01225     elem.setAttribute( "value", value() );
01226 }
01227 
01228 void KoCustomVariable::load( QDomElement& elem )
01229 {
01230     KoVariable::load( elem );
01231     QDomElement e = elem.namedItem( "CUSTOM" ).toElement();
01232     if (!e.isNull())
01233     {
01234         m_varValue = QVariant (e.attribute( "name" ));
01235         setValue( e.attribute( "value" ) );
01236     }
01237 }
01238 
01239 QString KoCustomVariable::value() const
01240 {
01241     return m_varColl->getVariableValue( m_varValue.toString() );
01242 }
01243 
01244 void KoCustomVariable::setValue( const QString &v )
01245 {
01246     m_varColl->setVariableValue( m_varValue.toString(), v );
01247 }
01248 
01249 QStringList KoCustomVariable::actionTexts()
01250 {
01251     return QStringList( i18n( "Custom..." ) );
01252 }
01253 
01254 void KoCustomVariable::recalc()
01255 {
01256     resize();
01257 }
01258 
01259 /******************************************************************/
01260 /* Class: KoMailMergeVariable                                  */
01261 /******************************************************************/
01262 KoMailMergeVariable::KoMailMergeVariable( KoTextDocument *textdoc, const QString &name, KoVariableFormat *varFormat,KoVariableCollection *_varColl )
01263     : KoVariable( textdoc, varFormat, _varColl )
01264 {
01265     m_varValue = QVariant ( name );
01266 }
01267 
01268 QString KoMailMergeVariable::fieldCode()
01269 {
01270     return i18n("Mail Merge");
01271 }
01272 
01273 
01274 void KoMailMergeVariable::saveVariable( QDomElement& parentElem )
01275 {
01276     QDomElement elem = parentElem.ownerDocument().createElement( "MAILMERGE" );
01277     parentElem.appendChild( elem );
01278     elem.setAttribute( "name", m_varValue.toString() );
01279 }
01280 
01281 void KoMailMergeVariable::load( QDomElement& elem )
01282 {
01283     KoVariable::load( elem );
01284     QDomElement e = elem.namedItem( "MAILMERGE" ).toElement();
01285     if (!e.isNull())
01286         m_varValue = QVariant( e.attribute( "name" ) );
01287 }
01288 
01289 QString KoMailMergeVariable::value() const
01290 {
01291     return QString();//m_doc->getMailMergeDataBase()->getValue( m_name );
01292 }
01293 
01294 QString KoMailMergeVariable::text(bool /*realValue*/)
01295 {
01296     // ## should use a format maybe
01297     QString v = value();
01298     if ( v == name() )
01299         return "<" + v + ">";
01300     return v;
01301 }
01302 
01303 QStringList KoMailMergeVariable::actionTexts()
01304 {
01305     return QStringList( i18n( "&Mail Merge..." ) );
01306 }
01307 
01308 /******************************************************************/
01309 /* Class: KoPgNumVariable                                         */
01310 /******************************************************************/
01311 KoPgNumVariable::KoPgNumVariable( KoTextDocument *textdoc, short int subtype, KoVariableFormat *varFormat,KoVariableCollection *_varColl )
01312         : KoVariable( textdoc, varFormat, _varColl ), m_subtype( subtype )
01313 {
01314 }
01315 
01316 QString KoPgNumVariable::fieldCode()
01317 {
01318     if ( m_subtype == VST_PGNUM_CURRENT )
01319         return i18n("Page Current Num");
01320     else if ( m_subtype == VST_PGNUM_TOTAL )
01321         return i18n("Total Page Num");
01322     else if ( m_subtype == VST_CURRENT_SECTION )
01323         return i18n("Current Section");
01324     else if ( m_subtype == VST_PGNUM_PREVIOUS )
01325         return i18n("Previous Page Number");
01326     else if ( m_subtype == VST_PGNUM_NEXT )
01327         return i18n("Next Page Number");
01328 
01329     else
01330         return i18n("Current Section");
01331 }
01332 
01333 
01334 void KoPgNumVariable::saveVariable( QDomElement& parentElem )
01335 {
01336     QDomElement pgNumElem = parentElem.ownerDocument().createElement( "PGNUM" );
01337     parentElem.appendChild( pgNumElem );
01338     pgNumElem.setAttribute( "subtype", m_subtype );
01339     if ( m_subtype != VST_CURRENT_SECTION )
01340         pgNumElem.setAttribute( "value", m_varValue.toInt() );
01341     else
01342         pgNumElem.setAttribute( "value", m_varValue.toString() );
01343 }
01344 
01345 void KoPgNumVariable::load( QDomElement& elem )
01346 {
01347     KoVariable::load( elem );
01348     QDomElement pgNumElem = elem.namedItem( "PGNUM" ).toElement();
01349     if (!pgNumElem.isNull())
01350     {
01351         m_subtype = pgNumElem.attribute("subtype").toInt();
01352         // ### This could use the format...
01353         if ( m_subtype != VST_CURRENT_SECTION )
01354             m_varValue = QVariant(pgNumElem.attribute("value").toInt());
01355         else
01356             m_varValue = QVariant(pgNumElem.attribute("value"));
01357     }
01358 }
01359 
01360 QStringList KoPgNumVariable::actionTexts()
01361 {
01362     QStringList lst;
01363     lst << i18n( "Page Number" );
01364     lst << i18n( "Number of Pages" );
01365     lst << i18n( "Section Title" );
01366     lst << i18n( "Previous Page" );
01367     lst << i18n( "Next Page" );
01368     return lst;
01369 }
01370 
01371 QStringList KoPgNumVariable::subTypeText()
01372 {
01373     return KoPgNumVariable::actionTexts();
01374 }
01375 
01376 void KoPgNumVariable::setVariableSubType( short int type )
01377 {
01378     m_subtype = type;
01379     Q_ASSERT( m_varColl );
01380     KoVariableFormatCollection* fc = m_varColl->formatCollection();
01381     setVariableFormat((m_subtype == VST_CURRENT_SECTION) ? fc->format("STRING") : fc->format("NUMBER"));
01382 }
01383 
01384 /******************************************************************/
01385 /* Class: KoFieldVariable                                         */
01386 /******************************************************************/
01387 KoFieldVariable::KoFieldVariable( KoTextDocument *textdoc, short int subtype, KoVariableFormat *varFormat, KoVariableCollection *_varColl ,KoDocument *_doc )
01388     : KoVariable( textdoc, varFormat,_varColl ), m_subtype( subtype ), m_doc(_doc)
01389 {
01390 }
01391 
01392 QString KoFieldVariable::fieldCode()
01393 {
01394     switch( m_subtype ) {
01395     case VST_FILENAME:
01396         return i18n("Filename");
01397         break;
01398     case VST_DIRECTORYNAME:
01399         return i18n("Directory Name");
01400         break;
01401     case VST_PATHFILENAME:
01402         return i18n("Path Filename");
01403         break;
01404     case VST_FILENAMEWITHOUTEXTENSION:
01405         return i18n("Filename Without Extension");
01406         break;
01407     case VST_AUTHORNAME:
01408         return i18n("Author Name");
01409         break;
01410     case VST_EMAIL:
01411         return i18n("Email");
01412         break;
01413     case VST_COMPANYNAME:
01414         return i18n("Company Name");
01415         break;
01416     case VST_TELEPHONE:
01417         return i18n("Telephone");
01418         break;
01419     case VST_FAX:
01420         return i18n("Fax");
01421         break;
01422     case VST_COUNTRY:
01423         return i18n("Country");
01424         break;
01425     case VST_POSTAL_CODE:
01426         return i18n("Postal Code");
01427         break;
01428     case VST_CITY:
01429         return i18n("City");
01430         break;
01431     case VST_STREET:
01432         return i18n("Street");
01433         break;
01434     case VST_AUTHORTITLE:
01435         return i18n("Author Title");
01436         break;
01437     case VST_TITLE:
01438         return i18n("Title");
01439         break;
01440     case VST_ABSTRACT:
01441         return i18n("Abstract");
01442         break;
01443     case VST_INITIAL:
01444         return i18n("Initials");
01445         break;
01446     }
01447     return i18n("Field");
01448 }
01449 
01450 QString KoFieldVariable::text(bool realValue)
01451 {
01452     if (m_varColl->variableSetting()->displayFieldCode()&&!realValue)
01453         return fieldCode();
01454     else
01455         return value();
01456 } // use a format when they are customizable
01457 
01458 
01459 void KoFieldVariable::saveVariable( QDomElement& parentElem )
01460 {
01461     //kdDebug(32500) << "KoFieldVariable::saveVariable" << endl;
01462     QDomElement elem = parentElem.ownerDocument().createElement( "FIELD" );
01463     parentElem.appendChild( elem );
01464     elem.setAttribute( "subtype", m_subtype );
01465     elem.setAttribute( "value", m_varValue.toString() );
01466 }
01467 
01468 void KoFieldVariable::load( QDomElement& elem )
01469 {
01470     KoVariable::load( elem );
01471     QDomElement e = elem.namedItem( "FIELD" ).toElement();
01472     if (!e.isNull())
01473     {
01474         m_subtype = e.attribute( "subtype" ).toInt();
01475         if ( m_subtype == VST_NONE )
01476             kdWarning() << "Field subtype of -1 found in the file !" << endl;
01477         m_varValue = QVariant( e.attribute( "value" ) );
01478     } else
01479         kdWarning() << "FIELD element not found !" << endl;
01480 }
01481 
01482 void KoFieldVariable::recalc()
01483 {
01484     QString value;
01485     switch( m_subtype ) {
01486         case VST_NONE:
01487             kdWarning() << "KoFieldVariable::recalc() called with m_subtype = VST_NONE !" << endl;
01488             break;
01489         case VST_FILENAME:
01490             value = m_doc->url().fileName();
01491             break;
01492         case VST_DIRECTORYNAME:
01493             value = m_doc->url().directory();
01494             break;
01495         case VST_PATHFILENAME:
01496             value=m_doc->url().path();
01497             break;
01498         case VST_FILENAMEWITHOUTEXTENSION:
01499         {
01500             QString file=m_doc->url().fileName();
01501             int pos=file.findRev(".");
01502             if(pos !=-1)
01503                 value=file.mid(0,pos);
01504             else
01505                 value=file;
01506         }
01507         break;
01508         case VST_AUTHORNAME:
01509         case VST_EMAIL:
01510         case VST_COMPANYNAME:
01511         case VST_TELEPHONE:
01512         case VST_FAX:
01513         case VST_COUNTRY:
01514         case VST_POSTAL_CODE:
01515         case VST_CITY:
01516         case VST_STREET:
01517         case VST_AUTHORTITLE:
01518         case VST_INITIAL:
01519         {
01520             KoDocumentInfo * info = m_doc->documentInfo();
01521             KoDocumentInfoAuthor * authorPage = static_cast<KoDocumentInfoAuthor *>(info->page( "author" ));
01522             if ( !authorPage )
01523                 kdWarning() << "Author information not found in documentInfo !" << endl;
01524             else
01525             {
01526                 if ( m_subtype == VST_AUTHORNAME )
01527                     value = authorPage->fullName();
01528                 else if ( m_subtype == VST_EMAIL )
01529                     value = authorPage->email();
01530                 else if ( m_subtype == VST_COMPANYNAME )
01531                     value = authorPage->company();
01532                 else if ( m_subtype == VST_TELEPHONE )
01533                     value = authorPage->telephone();
01534                 else if ( m_subtype == VST_FAX )
01535                     value = authorPage->fax();
01536                 else if ( m_subtype == VST_COUNTRY )
01537                     value = authorPage->country();
01538                 else if ( m_subtype == VST_POSTAL_CODE )
01539                     value = authorPage->postalCode();
01540                 else if ( m_subtype == VST_CITY )
01541                     value = authorPage->city();
01542                 else if ( m_subtype == VST_STREET )
01543                     value = authorPage->street();
01544                 else if ( m_subtype == VST_AUTHORTITLE )
01545                     value = authorPage->title();
01546                 else if ( m_subtype == VST_INITIAL )
01547                     value = authorPage->initial();
01548             }
01549         }
01550         break;
01551         case VST_TITLE:
01552         case VST_ABSTRACT:
01553         {
01554             KoDocumentInfo * info = m_doc->documentInfo();
01555             KoDocumentInfoAbout * aboutPage = static_cast<KoDocumentInfoAbout *>(info->page( "about" ));
01556             if ( !aboutPage )
01557                 kdWarning() << "'About' page not found in documentInfo !" << endl;
01558             else
01559             {
01560                 if ( m_subtype == VST_TITLE )
01561                     value = aboutPage->title();
01562                 else
01563                     value = aboutPage->abstract();
01564             }
01565         }
01566         break;
01567     }
01568 
01569     if (value.isEmpty())        // try the initial value
01570         value = m_varValue.toString();
01571 
01572     if (value.isEmpty())        // still empty? give up
01573         value = i18n("<None>");
01574 
01575     m_varValue = QVariant( value );
01576 
01577     resize();
01578 }
01579 
01580 QStringList KoFieldVariable::actionTexts()
01581 {
01582     // NOTE: if you change here, also change fieldSubType()
01583     QStringList lst;
01584     lst << i18n( "Author Name" );
01585     lst << i18n( "Title" );
01586     lst << i18n( "Company" );
01587     lst << i18n( "Email" );
01588     lst << i18n( "Telephone");
01589     lst << i18n( "Fax");
01590     lst << i18n( "Street" );
01591     lst << i18n( "Postal Code" );
01592     lst << i18n( "City" );
01593     lst << i18n( "Country");
01594 
01595     lst << i18n( "Document Title" );
01596     lst << i18n( "Document Abstract" );
01597 
01598     lst << i18n( "File Name" );
01599     lst << i18n( "File Name without Extension" );
01600     lst << i18n( "Directory Name" ); // is "Name" necessary ?
01601     lst << i18n( "Directory && File Name" );
01602     lst << i18n( "Initials" );
01603     return lst;
01604 }
01605 
01606 short int KoFieldVariable::variableSubType( short int menuNumber )
01607 {
01608     return fieldSubType(menuNumber);
01609 }
01610 
01611 KoFieldVariable::FieldSubType KoFieldVariable::fieldSubType(short int menuNumber)
01612 {
01613     // NOTE: if you change here, also change actionTexts()
01614     FieldSubType v;
01615     switch (menuNumber)
01616     {
01617         case 0: v = VST_AUTHORNAME;
01618                 break;
01619         case 1: v = VST_AUTHORTITLE;
01620                 break;
01621         case 2: v = VST_COMPANYNAME;
01622                 break;
01623         case 3: v = VST_EMAIL;
01624                 break;
01625         case 4: v = VST_TELEPHONE;
01626                 break;
01627         case 5: v = VST_FAX;
01628                 break;
01629         case 6: v = VST_STREET;
01630                 break;
01631         case 7: v = VST_POSTAL_CODE;
01632                 break;
01633         case 8: v = VST_CITY;
01634                 break;
01635         case 9: v = VST_COUNTRY;
01636                 break;
01637         case 10: v = VST_TITLE;
01638                 break;
01639         case 11: v = VST_ABSTRACT;
01640                 break;
01641         case 12: v = VST_FILENAME;
01642                 break;
01643         case 13: v = VST_FILENAMEWITHOUTEXTENSION;
01644                 break;
01645         case 14: v = VST_DIRECTORYNAME;
01646                 break;
01647         case 15: v = VST_PATHFILENAME;
01648                 break;
01649         case 16: v = VST_INITIAL;
01650                 break;
01651         default:
01652             v = VST_NONE;
01653             break;
01654     }
01655     return v;
01656 }
01657 
01658 QStringList KoFieldVariable::subTypeText()
01659 {
01660     return KoFieldVariable::actionTexts();
01661 }
01662 
01663 /******************************************************************/
01664 /* Class: KoLinkVariable                                          */
01665 /******************************************************************/
01666 KoLinkVariable::KoLinkVariable( KoTextDocument *textdoc, const QString & _linkName, const QString & _ulr,KoVariableFormat *varFormat,KoVariableCollection *_varColl )
01667     : KoVariable( textdoc, varFormat,_varColl )
01668     ,m_url(_ulr)
01669 {
01670     m_varValue = QVariant( _linkName );
01671 }
01672 
01673 QString KoLinkVariable::fieldCode()
01674 {
01675     return i18n("Link");
01676 }
01677 
01678 QString KoLinkVariable::text(bool realValue)
01679 {
01680     if (m_varColl->variableSetting()->displayFieldCode()&&!realValue)
01681         return fieldCode();
01682     else
01683         return value();
01684 }
01685 
01686 void KoLinkVariable::saveVariable( QDomElement& parentElem )
01687 {
01688     QDomElement linkElem = parentElem.ownerDocument().createElement( "LINK" );
01689     parentElem.appendChild( linkElem );
01690     linkElem.setAttribute( "linkName", m_varValue.toString() );
01691     linkElem.setAttribute( "hrefName", m_url );
01692 }
01693 
01694 void KoLinkVariable::load( QDomElement& elem )
01695 {
01696     KoVariable::load( elem );
01697     QDomElement linkElem = elem.namedItem( "LINK" ).toElement();
01698     if (!linkElem.isNull())
01699     {
01700         m_varValue = QVariant(linkElem.attribute("linkName"));
01701         m_url = linkElem.attribute("hrefName");
01702     }
01703 }
01704 
01705 void KoLinkVariable::recalc()
01706 {
01707     resize();
01708 }
01709 
01710 QStringList KoLinkVariable::actionTexts()
01711 {
01712     return QStringList( i18n( "Link..." ) );
01713 }
01714 
01715 
01716 void KoLinkVariable::drawCustomItem( QPainter* p, int x, int y, int wpix, int hpix, int ascentpix, int /*cx*/, int /*cy*/, int /*cw*/, int /*ch*/, const QColorGroup& cg, bool selected, int offset, bool drawingShadow )
01717 {
01718     KoTextFormat * fmt = format();
01719     KoZoomHandler * zh = textDocument()->paintingZoomHandler();
01720 
01721     bool displayLink = m_varColl->variableSetting()->displayLink();
01722     QFont font( fmt->screenFont( zh ) );
01723     if ( m_varColl->variableSetting()->underlineLink() )
01724         font.setUnderline( true );
01725     QColor textColor = displayLink ? cg.color( QColorGroup::Link ) : fmt->color();
01726 
01727     drawCustomItemHelper( p, x, y, wpix, hpix, ascentpix, cg, selected, offset, fmt, font, textColor, drawingShadow );
01728 }
01729 
01730 
01731 /******************************************************************/
01732 /* Class: KoNoteVariable                                          */
01733 /******************************************************************/
01734 KoNoteVariable::KoNoteVariable( KoTextDocument *textdoc, const QString & _note,KoVariableFormat *varFormat,KoVariableCollection *_varColl )
01735     : KoVariable( textdoc, varFormat,_varColl )
01736 {
01737     m_varValue = QVariant( _note );
01738 }
01739 
01740 QString KoNoteVariable::fieldCode()
01741 {
01742     return i18n("Note");
01743 }
01744 
01745 void KoNoteVariable::saveVariable( QDomElement& parentElem )
01746 {
01747     QDomElement linkElem = parentElem.ownerDocument().createElement( "NOTE" );
01748     parentElem.appendChild( linkElem );
01749     linkElem.setAttribute( "note", m_varValue.toString() );
01750 }
01751 
01752 void KoNoteVariable::load( QDomElement& elem )
01753 {
01754     KoVariable::load( elem );
01755     QDomElement linkElem = elem.namedItem( "NOTE" ).toElement();
01756     if (!linkElem.isNull())
01757     {
01758         m_varValue = QVariant(linkElem.attribute("note"));
01759     }
01760 }
01761 
01762 void KoNoteVariable::recalc()
01763 {
01764     resize();
01765 }
01766 
01767 QStringList KoNoteVariable::actionTexts()
01768 {
01769     return QStringList( i18n( "Note..." ) );
01770 }
01771 
01772 QString KoNoteVariable::text(bool realValue)
01773 {
01774     if (m_varColl->variableSetting()->displayComment() &&
01775         m_varColl->variableSetting()->displayFieldCode()&&!realValue)
01776         return fieldCode();
01777     else
01778         //for a note return just a "space" we can look at
01779         //note when we "right button"
01780         return QString(" ");
01781 
01782 }
01783 
01784 void KoNoteVariable::drawCustomItem( QPainter* p, int x, int y, int wpix, int hpix, int ascentpix, int cx, int cy, int cw, int ch, const QColorGroup& cg, bool selected, int offset, bool drawingShadow )
01785 {
01786     if ( !m_varColl->variableSetting()->displayComment())
01787         return;
01788 
01789     KoTextFormat * fmt = format();
01790     //kdDebug(32500) << "KoNoteVariable::drawCustomItem index=" << index() << " x=" << x << " y=" << y << endl;
01791 
01792     p->save();
01793     p->setPen( QPen( fmt->color() ) );
01794     if ( fmt->textBackgroundColor().isValid() )
01795         p->fillRect( x, y, wpix, hpix, fmt->textBackgroundColor() );
01796     if ( selected )
01797     {
01798         p->setPen( QPen( cg.color( QColorGroup::HighlightedText ) ) );
01799         p->fillRect( x, y, wpix, hpix, cg.color( QColorGroup::Highlight ) );
01800     }
01801     else if ( textDocument() && p->device()->devType() != QInternal::Printer
01802         && !textDocument()->dontDrawingNoteVariable())
01803     {
01804         p->fillRect( x, y, wpix, hpix, Qt::yellow);
01805         p->setPen( QPen( cg.color( QColorGroup::Highlight ), 0, Qt::DotLine ) );
01806         p->drawRect( x, y, wpix, hpix );
01807     }
01808     //call it for use drawCustomItemHelper just for draw font effect
01809     KoVariable::drawCustomItem( p, x, y, wpix, hpix, ascentpix, cx, cy, cw, ch, cg, selected, offset, drawingShadow );
01810 
01811     p->restore();
01812 }
01813 
01814 void KoPgNumVariable::setSectionTitle( const QString& _title )
01815 {
01816     QString title( _title );
01817     if ( title.isEmpty() )
01818     {
01819         title = i18n("<None>"); // TODO after msg freeze: <No title>
01820     }
01821     m_varValue = QVariant( title );
01822 }
01823 
KDE Logo
This file is part of the documentation for lib Library Version 1.3.5.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Mar 20 14:25:28 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003