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         return 0;
01013     }
01014     config->setGroup("Date format history");
01015     stringList.remove(string);
01016     stringList.prepend(string);
01017     for(int i=0;i<=count;i++)
01018     {
01019         QString num;
01020         num.setNum(i);
01021         config->writeEntry("Last Used"+num, stringList[i]);
01022     }
01023     config->sync();
01024     delete dialog;
01025     return QCString(QCString("DATE") + string );
01026 }
01027 
01028 /******************************************************************/
01029 /* Class: KoTimeVariable                                          */
01030 /******************************************************************/
01031 KoTimeVariable::KoTimeVariable( KoTextDocument *textdoc, short int subtype, KoVariableFormat *varFormat, KoVariableCollection *_varColl, int _correct)
01032     : KoVariable( textdoc, varFormat,_varColl ), m_subtype( subtype ), m_correctTime( _correct)
01033 {
01034 }
01035 
01036 QString KoTimeVariable::fieldCode()
01037 {
01038     return (m_subtype == VST_TIME_FIX)?i18n("Time (Fixed)"):i18n("Time");
01039 }
01040 
01041 
01042 void KoTimeVariable::resize()
01043 {
01044     KoTextFormat * fmt = format();
01045     if ( !fmt->language().isEmpty() )
01046     {
01047         QString oldLanguage = KGlobal::locale()->language();
01048         bool changeLanguage = KGlobal::locale()->setLanguage( fmt->language() );
01049         KoVariable::resize();
01050         if ( changeLanguage )
01051             KGlobal::locale()->setLanguage( oldLanguage );
01052     }
01053     else
01054         KoVariable::resize();
01055 }
01056 
01057 void KoTimeVariable::recalc()
01058 {
01059     if ( m_subtype == VST_TIME_CURRENT )
01060         m_varValue = QVariant( QTime::currentTime().addSecs(60*m_correctTime));
01061     else
01062     {
01063         // Only if never set before (i.e. upon insertion)
01064         if ( m_varValue.toTime().isNull() )
01065             m_varValue = QVariant( QTime::currentTime().addSecs(60*m_correctTime));
01066     }
01067     resize();
01068 }
01069 
01070 
01071 void KoTimeVariable::saveVariable( QDomElement& parentElem )
01072 {
01073     QDomElement elem = parentElem.ownerDocument().createElement( "TIME" );
01074     parentElem.appendChild( elem );
01075     QTime time = m_varValue.toTime();
01076     time = time.addSecs(-60*m_correctTime);
01077     elem.setAttribute( "hour", time.hour() );
01078     elem.setAttribute( "minute", time.minute() );
01079     elem.setAttribute( "second", time.second() );
01080     elem.setAttribute( "msecond", time.msec() );
01081     elem.setAttribute( "fix", m_subtype == VST_TIME_FIX );
01082     elem.setAttribute( "correct", m_correctTime );
01083 }
01084 
01085 void KoTimeVariable::load( QDomElement& elem )
01086 {
01087     KoVariable::load( elem );
01088 
01089     QDomElement e = elem.namedItem( "TIME" ).toElement();
01090     if (!e.isNull())
01091     {
01092         int h = e.attribute("hour").toInt();
01093         int m = e.attribute("minute").toInt();
01094         int s = e.attribute("second").toInt();
01095         int ms = e.attribute("msecond").toInt();
01096         int correct = 0;
01097         if ( e.hasAttribute("correct"))
01098             correct=e.attribute("correct").toInt();
01099         bool fix = static_cast<bool>( e.attribute("fix").toInt() );
01100         if ( fix )
01101         {
01102             QTime time;
01103             time.setHMS( h, m, s, ms );
01104             time = time.addSecs( 60*m_correctTime );
01105             m_varValue = QVariant( time);
01106 
01107         }
01108         m_subtype = fix ? VST_TIME_FIX : VST_TIME_CURRENT;
01109         m_correctTime = correct;
01110     }
01111 }
01112 
01113 QStringList KoTimeVariable::actionTexts()
01114 {
01115     QStringList lst;
01116     lst << i18n( "Current Time (fixed)" );
01117     lst << i18n( "Current Time (variable)" );
01118     return lst;
01119 }
01120 
01121 QStringList KoTimeVariable::subTypeText()
01122 {
01123     return KoTimeVariable::actionTexts();
01124 }
01125 
01126 QCString KoTimeVariable::formatStr(int & _correct)
01127 {
01128     QCString string;
01129     QStringList stringList;
01130     KDialogBase* dialog=new KDialogBase(0, 0, true, i18n("Time Format"), KDialogBase::Ok|KDialogBase::Cancel);
01131     TimeFormatWidget* widget=new TimeFormatWidget(dialog);
01132     dialog->setMainWidget(widget);
01133     KConfig* config = KoGlobal::kofficeConfig();
01134     int count=0;
01135     if( config->hasGroup("Time format history") )
01136     {
01137         KConfigGroupSaver cgs( config, "Time format history" );
01138         const int noe=config->readNumEntry("Number Of Entries", 5);
01139         for(int i=0;i<noe;i++)
01140         {
01141             QString num;
01142             num.setNum(i);
01143             QString tmpString(config->readEntry("Last Used"+num));
01144             if(tmpString.startsWith("locale"))
01145                 continue;
01146             else if(stringList.contains(tmpString))
01147                 continue;
01148             else if(!tmpString.isEmpty())
01149             {
01150                 stringList.append(tmpString);
01151                 count++;
01152             }
01153         }
01154     }
01155     if(!stringList.isEmpty())
01156     {
01157         widget->combo1->insertItem("---");
01158         widget->combo1->insertStringList(stringList);
01159     }
01160     if(false) // ### TODO: select the last used item
01161     {
01162         QComboBox *combo= widget->combo1;
01163         combo->setCurrentItem(combo->count() -1);
01164     }
01165     if(dialog->exec()==QDialog::Accepted)
01166     {
01167         string = widget->resultString().utf8();
01168         _correct = widget->correctValue();
01169     }
01170     else
01171     {
01172         return 0;
01173     }
01174     config->setGroup("Time format history");
01175     stringList.remove(string);
01176     stringList.prepend(string);
01177     for(int i=0;i<=count;i++)
01178     {
01179         QString num;
01180         num.setNum(i);
01181         config->writeEntry("Last Used"+num, stringList[i]);
01182     }
01183     config->sync();
01184     delete dialog;
01185     return QCString("TIME"+string );
01186 }
01187 
01188 QCString KoTimeVariable::defaultFormat()
01189 {
01190     return QCString(QCString("TIME")+QCString("locale") );
01191 }
01192 
01193 
01194 /******************************************************************/
01195 /* Class: KoCustomVariable                                        */
01196 /******************************************************************/
01197 KoCustomVariable::KoCustomVariable( KoTextDocument *textdoc, const QString &name, KoVariableFormat *varFormat, KoVariableCollection *_varColl )
01198     : KoVariable( textdoc, varFormat,_varColl )
01199 {
01200     m_varValue = QVariant( name );
01201 }
01202 
01203 QString KoCustomVariable::fieldCode()
01204 {
01205     return i18n("Custom Variable");
01206 }
01207 
01208 QString KoCustomVariable::text(bool realValue)
01209 {
01210     if (m_varColl->variableSetting()->displayFieldCode()&&!realValue)
01211         return fieldCode();
01212     else
01213         return value();
01214 } // use a format when they are customizable
01215 
01216 
01217 
01218 void KoCustomVariable::saveVariable( QDomElement& parentElem )
01219 {
01220     QDomElement elem = parentElem.ownerDocument().createElement( "CUSTOM" );
01221     parentElem.appendChild( elem );
01222     elem.setAttribute( "name", m_varValue.toString() );
01223     elem.setAttribute( "value", value() );
01224 }
01225 
01226 void KoCustomVariable::load( QDomElement& elem )
01227 {
01228     KoVariable::load( elem );
01229     QDomElement e = elem.namedItem( "CUSTOM" ).toElement();
01230     if (!e.isNull())
01231     {
01232         m_varValue = QVariant (e.attribute( "name" ));
01233         setValue( e.attribute( "value" ) );
01234     }
01235 }
01236 
01237 QString KoCustomVariable::value() const
01238 {
01239     return m_varColl->getVariableValue( m_varValue.toString() );
01240 }
01241 
01242 void KoCustomVariable::setValue( const QString &v )
01243 {
01244     m_varColl->setVariableValue( m_varValue.toString(), v );
01245 }
01246 
01247 QStringList KoCustomVariable::actionTexts()
01248 {
01249     return QStringList( i18n( "Custom..." ) );
01250 }
01251 
01252 void KoCustomVariable::recalc()
01253 {
01254     resize();
01255 }
01256 
01257 /******************************************************************/
01258 /* Class: KoMailMergeVariable                                  */
01259 /******************************************************************/
01260 KoMailMergeVariable::KoMailMergeVariable( KoTextDocument *textdoc, const QString &name, KoVariableFormat *varFormat,KoVariableCollection *_varColl )
01261     : KoVariable( textdoc, varFormat, _varColl )
01262 {
01263     m_varValue = QVariant ( name );
01264 }
01265 
01266 QString KoMailMergeVariable::fieldCode()
01267 {
01268     return i18n("Mail Merge");
01269 }
01270 
01271 
01272 void KoMailMergeVariable::saveVariable( QDomElement& parentElem )
01273 {
01274     QDomElement elem = parentElem.ownerDocument().createElement( "MAILMERGE" );
01275     parentElem.appendChild( elem );
01276     elem.setAttribute( "name", m_varValue.toString() );
01277 }
01278 
01279 void KoMailMergeVariable::load( QDomElement& elem )
01280 {
01281     KoVariable::load( elem );
01282     QDomElement e = elem.namedItem( "MAILMERGE" ).toElement();
01283     if (!e.isNull())
01284         m_varValue = QVariant( e.attribute( "name" ) );
01285 }
01286 
01287 QString KoMailMergeVariable::value() const
01288 {
01289     return QString();//m_doc->getMailMergeDataBase()->getValue( m_name );
01290 }
01291 
01292 QString KoMailMergeVariable::text(bool /*realValue*/)
01293 {
01294     // ## should use a format maybe
01295     QString v = value();
01296     if ( v == name() )
01297         return "<" + v + ">";
01298     return v;
01299 }
01300 
01301 QStringList KoMailMergeVariable::actionTexts()
01302 {
01303     return QStringList( i18n( "&Mail Merge..." ) );
01304 }
01305 
01306 /******************************************************************/
01307 /* Class: KoPgNumVariable                                         */
01308 /******************************************************************/
01309 KoPgNumVariable::KoPgNumVariable( KoTextDocument *textdoc, short int subtype, KoVariableFormat *varFormat,KoVariableCollection *_varColl )
01310         : KoVariable( textdoc, varFormat, _varColl ), m_subtype( subtype )
01311 {
01312 }
01313 
01314 QString KoPgNumVariable::fieldCode()
01315 {
01316     if ( m_subtype == VST_PGNUM_CURRENT )
01317         return i18n("Page Current Num");
01318     else if ( m_subtype == VST_PGNUM_TOTAL )
01319         return i18n("Total Page Num");
01320     else if ( m_subtype == VST_CURRENT_SECTION )
01321         return i18n("Current Section");
01322     else if ( m_subtype == VST_PGNUM_PREVIOUS )
01323         return i18n("Previous Page Number");
01324     else if ( m_subtype == VST_PGNUM_NEXT )
01325         return i18n("Next Page Number");
01326 
01327     else
01328         return i18n("Current Section");
01329 }
01330 
01331 
01332 void KoPgNumVariable::saveVariable( QDomElement& parentElem )
01333 {
01334     QDomElement pgNumElem = parentElem.ownerDocument().createElement( "PGNUM" );
01335     parentElem.appendChild( pgNumElem );
01336     pgNumElem.setAttribute( "subtype", m_subtype );
01337     if ( m_subtype != VST_CURRENT_SECTION )
01338         pgNumElem.setAttribute( "value", m_varValue.toInt() );
01339     else
01340         pgNumElem.setAttribute( "value", m_varValue.toString() );
01341 }
01342 
01343 void KoPgNumVariable::load( QDomElement& elem )
01344 {
01345     KoVariable::load( elem );
01346     QDomElement pgNumElem = elem.namedItem( "PGNUM" ).toElement();
01347     if (!pgNumElem.isNull())
01348     {
01349         m_subtype = pgNumElem.attribute("subtype").toInt();
01350         // ### This could use the format...
01351         if ( m_subtype != VST_CURRENT_SECTION )
01352             m_varValue = QVariant(pgNumElem.attribute("value").toInt());
01353         else
01354             m_varValue = QVariant(pgNumElem.attribute("value"));
01355     }
01356 }
01357 
01358 QStringList KoPgNumVariable::actionTexts()
01359 {
01360     QStringList lst;
01361     lst << i18n( "Page Number" );
01362     lst << i18n( "Number of Pages" );
01363     lst << i18n( "Section Title" );
01364     lst << i18n( "Previous Page" );
01365     lst << i18n( "Next Page" );
01366     return lst;
01367 }
01368 
01369 QStringList KoPgNumVariable::subTypeText()
01370 {
01371     return KoPgNumVariable::actionTexts();
01372 }
01373 
01374 void KoPgNumVariable::setVariableSubType( short int type )
01375 {
01376     m_subtype = type;
01377     Q_ASSERT( m_varColl );
01378     KoVariableFormatCollection* fc = m_varColl->formatCollection();
01379     setVariableFormat((m_subtype == VST_CURRENT_SECTION) ? fc->format("STRING") : fc->format("NUMBER"));
01380 }
01381 
01382 /******************************************************************/
01383 /* Class: KoFieldVariable                                         */
01384 /******************************************************************/
01385 KoFieldVariable::KoFieldVariable( KoTextDocument *textdoc, short int subtype, KoVariableFormat *varFormat, KoVariableCollection *_varColl ,KoDocument *_doc )
01386     : KoVariable( textdoc, varFormat,_varColl ), m_subtype( subtype ), m_doc(_doc)
01387 {
01388 }
01389 
01390 QString KoFieldVariable::fieldCode()
01391 {
01392     switch( m_subtype ) {
01393     case VST_FILENAME:
01394         return i18n("Filename");
01395         break;
01396     case VST_DIRECTORYNAME:
01397         return i18n("Directory Name");
01398         break;
01399     case VST_PATHFILENAME:
01400         return i18n("Path Filename");
01401         break;
01402     case VST_FILENAMEWITHOUTEXTENSION:
01403         return i18n("Filename Without Extension");
01404         break;
01405     case VST_AUTHORNAME:
01406         return i18n("Author Name");
01407         break;
01408     case VST_EMAIL:
01409         return i18n("Email");
01410         break;
01411     case VST_COMPANYNAME:
01412         return i18n("Company Name");
01413         break;
01414     case VST_TELEPHONE:
01415         return i18n("Telephone");
01416         break;
01417     case VST_FAX:
01418         return i18n("Fax");
01419         break;
01420     case VST_COUNTRY:
01421         return i18n("Country");
01422         break;
01423     case VST_POSTAL_CODE:
01424         return i18n("Postal Code");
01425         break;
01426     case VST_CITY:
01427         return i18n("City");
01428         break;
01429     case VST_STREET:
01430         return i18n("Street");
01431         break;
01432     case VST_AUTHORTITLE:
01433         return i18n("Author Title");
01434         break;
01435     case VST_TITLE:
01436         return i18n("Title");
01437         break;
01438     case VST_ABSTRACT:
01439         return i18n("Abstract");
01440         break;
01441     case VST_INITIAL:
01442         return i18n("Initials");
01443         break;
01444     }
01445     return i18n("Field");
01446 }
01447 
01448 QString KoFieldVariable::text(bool realValue)
01449 {
01450     if (m_varColl->variableSetting()->displayFieldCode()&&!realValue)
01451         return fieldCode();
01452     else
01453         return value();
01454 } // use a format when they are customizable
01455 
01456 
01457 void KoFieldVariable::saveVariable( QDomElement& parentElem )
01458 {
01459     //kdDebug(32500) << "KoFieldVariable::saveVariable" << endl;
01460     QDomElement elem = parentElem.ownerDocument().createElement( "FIELD" );
01461     parentElem.appendChild( elem );
01462     elem.setAttribute( "subtype", m_subtype );
01463     elem.setAttribute( "value", m_varValue.toString() );
01464 }
01465 
01466 void KoFieldVariable::load( QDomElement& elem )
01467 {
01468     KoVariable::load( elem );
01469     QDomElement e = elem.namedItem( "FIELD" ).toElement();
01470     if (!e.isNull())
01471     {
01472         m_subtype = e.attribute( "subtype" ).toInt();
01473         if ( m_subtype == VST_NONE )
01474             kdWarning() << "Field subtype of -1 found in the file !" << endl;
01475         m_varValue = QVariant( e.attribute( "value" ) );
01476     } else
01477         kdWarning() << "FIELD element not found !" << endl;
01478 }
01479 
01480 void KoFieldVariable::recalc()
01481 {
01482     QString value;
01483     switch( m_subtype ) {
01484         case VST_NONE:
01485             kdWarning() << "KoFieldVariable::recalc() called with m_subtype = VST_NONE !" << endl;
01486             break;
01487         case VST_FILENAME:
01488             value = m_doc->url().fileName();
01489             break;
01490         case VST_DIRECTORYNAME:
01491             value = m_doc->url().directory();
01492             break;
01493         case VST_PATHFILENAME:
01494             value=m_doc->url().path();
01495             break;
01496         case VST_FILENAMEWITHOUTEXTENSION:
01497         {
01498             QString file=m_doc->url().fileName();
01499             int pos=file.findRev(".");
01500             if(pos !=-1)
01501                 value=file.mid(0,pos);
01502             else
01503                 value=file;
01504         }
01505         break;
01506         case VST_AUTHORNAME:
01507         case VST_EMAIL:
01508         case VST_COMPANYNAME:
01509         case VST_TELEPHONE:
01510         case VST_FAX:
01511         case VST_COUNTRY:
01512         case VST_POSTAL_CODE:
01513         case VST_CITY:
01514         case VST_STREET:
01515         case VST_AUTHORTITLE:
01516         case VST_INITIAL:
01517         {
01518             KoDocumentInfo * info = m_doc->documentInfo();
01519             KoDocumentInfoAuthor * authorPage = static_cast<KoDocumentInfoAuthor *>(info->page( "author" ));
01520             if ( !authorPage )
01521                 kdWarning() << "Author information not found in documentInfo !" << endl;
01522             else
01523             {
01524                 if ( m_subtype == VST_AUTHORNAME )
01525                     value = authorPage->fullName();
01526                 else if ( m_subtype == VST_EMAIL )
01527                     value = authorPage->email();
01528                 else if ( m_subtype == VST_COMPANYNAME )
01529                     value = authorPage->company();
01530                 else if ( m_subtype == VST_TELEPHONE )
01531                     value = authorPage->telephone();
01532                 else if ( m_subtype == VST_FAX )
01533                     value = authorPage->fax();
01534                 else if ( m_subtype == VST_COUNTRY )
01535                     value = authorPage->country();
01536                 else if ( m_subtype == VST_POSTAL_CODE )
01537                     value = authorPage->postalCode();
01538                 else if ( m_subtype == VST_CITY )
01539                     value = authorPage->city();
01540                 else if ( m_subtype == VST_STREET )
01541                     value = authorPage->street();
01542                 else if ( m_subtype == VST_AUTHORTITLE )
01543                     value = authorPage->title();
01544                 else if ( m_subtype == VST_INITIAL )
01545                     value = authorPage->initial();
01546             }
01547         }
01548         break;
01549         case VST_TITLE:
01550         case VST_ABSTRACT:
01551         {
01552             KoDocumentInfo * info = m_doc->documentInfo();
01553             KoDocumentInfoAbout * aboutPage = static_cast<KoDocumentInfoAbout *>(info->page( "about" ));
01554             if ( !aboutPage )
01555                 kdWarning() << "'About' page not found in documentInfo !" << endl;
01556             else
01557             {
01558                 if ( m_subtype == VST_TITLE )
01559                     value = aboutPage->title();
01560                 else
01561                     value = aboutPage->abstract();
01562             }
01563         }
01564         break;
01565     }
01566 
01567     if (value.isEmpty())        // try the initial value
01568         value = m_varValue.toString();
01569 
01570     if (value.isEmpty())        // still empty? give up
01571         value = i18n("<None>");
01572 
01573     m_varValue = QVariant( value );
01574 
01575     resize();
01576 }
01577 
01578 QStringList KoFieldVariable::actionTexts()
01579 {
01580     // NOTE: if you change here, also change fieldSubType()
01581     QStringList lst;
01582     lst << i18n( "Author Name" );
01583     lst << i18n( "Title" );
01584     lst << i18n( "Company" );
01585     lst << i18n( "Email" );
01586     lst << i18n( "Telephone");
01587     lst << i18n( "Fax");
01588     lst << i18n( "Street" );
01589     lst << i18n( "Postal Code" );
01590     lst << i18n( "City" );
01591     lst << i18n( "Country");
01592 
01593     lst << i18n( "Document Title" );
01594     lst << i18n( "Document Abstract" );
01595 
01596     lst << i18n( "File Name" );
01597     lst << i18n( "File Name without Extension" );
01598     lst << i18n( "Directory Name" ); // is "Name" necessary ?
01599     lst << i18n( "Directory && File Name" );
01600     lst << i18n( "Initials" );
01601     return lst;
01602 }
01603 
01604 short int KoFieldVariable::variableSubType( short int menuNumber )
01605 {
01606     return fieldSubType(menuNumber);
01607 }
01608 
01609 KoFieldVariable::FieldSubType KoFieldVariable::fieldSubType(short int menuNumber)
01610 {
01611     // NOTE: if you change here, also change actionTexts()
01612     FieldSubType v;
01613     switch (menuNumber)
01614     {
01615         case 0: v = VST_AUTHORNAME;
01616                 break;
01617         case 1: v = VST_AUTHORTITLE;
01618                 break;
01619         case 2: v = VST_COMPANYNAME;
01620                 break;
01621         case 3: v = VST_EMAIL;
01622                 break;
01623         case 4: v = VST_TELEPHONE;
01624                 break;
01625         case 5: v = VST_FAX;
01626                 break;
01627         case 6: v = VST_STREET;
01628                 break;
01629         case 7: v = VST_POSTAL_CODE;
01630                 break;
01631         case 8: v = VST_CITY;
01632                 break;
01633         case 9: v = VST_COUNTRY;
01634                 break;
01635         case 10: v = VST_TITLE;
01636                 break;
01637         case 11: v = VST_ABSTRACT;
01638                 break;
01639         case 12: v = VST_FILENAME;
01640                 break;
01641         case 13: v = VST_FILENAMEWITHOUTEXTENSION;
01642                 break;
01643         case 14: v = VST_DIRECTORYNAME;
01644                 break;
01645         case 15: v = VST_PATHFILENAME;
01646                 break;
01647         case 16: v = VST_INITIAL;
01648                 break;
01649         default:
01650             v = VST_NONE;
01651             break;
01652     }
01653     return v;
01654 }
01655 
01656 QStringList KoFieldVariable::subTypeText()
01657 {
01658     return KoFieldVariable::actionTexts();
01659 }
01660 
01661 /******************************************************************/
01662 /* Class: KoLinkVariable                                          */
01663 /******************************************************************/
01664 KoLinkVariable::KoLinkVariable( KoTextDocument *textdoc, const QString & _linkName, const QString & _ulr,KoVariableFormat *varFormat,KoVariableCollection *_varColl )
01665     : KoVariable( textdoc, varFormat,_varColl )
01666     ,m_url(_ulr)
01667 {
01668     m_varValue = QVariant( _linkName );
01669 }
01670 
01671 QString KoLinkVariable::fieldCode()
01672 {
01673     return i18n("Link");
01674 }
01675 
01676 QString KoLinkVariable::text(bool realValue)
01677 {
01678     if (m_varColl->variableSetting()->displayFieldCode()&&!realValue)
01679         return fieldCode();
01680     else
01681         return value();
01682 }
01683 
01684 void KoLinkVariable::saveVariable( QDomElement& parentElem )
01685 {
01686     QDomElement linkElem = parentElem.ownerDocument().createElement( "LINK" );
01687     parentElem.appendChild( linkElem );
01688     linkElem.setAttribute( "linkName", m_varValue.toString() );
01689     linkElem.setAttribute( "hrefName", m_url );
01690 }
01691 
01692 void KoLinkVariable::load( QDomElement& elem )
01693 {
01694     KoVariable::load( elem );
01695     QDomElement linkElem = elem.namedItem( "LINK" ).toElement();
01696     if (!linkElem.isNull())
01697     {
01698         m_varValue = QVariant(linkElem.attribute("linkName"));
01699         m_url = linkElem.attribute("hrefName");
01700     }
01701 }
01702 
01703 void KoLinkVariable::recalc()
01704 {
01705     resize();
01706 }
01707 
01708 QStringList KoLinkVariable::actionTexts()
01709 {
01710     return QStringList( i18n( "Link..." ) );
01711 }
01712 
01713 
01714 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 )
01715 {
01716     KoTextFormat * fmt = format();
01717     KoZoomHandler * zh = textDocument()->paintingZoomHandler();
01718 
01719     bool displayLink = m_varColl->variableSetting()->displayLink();
01720     QFont font( fmt->screenFont( zh ) );
01721     if ( m_varColl->variableSetting()->underlineLink() )
01722         font.setUnderline( true );
01723     QColor textColor = displayLink ? cg.color( QColorGroup::Link ) : fmt->color();
01724 
01725     drawCustomItemHelper( p, x, y, wpix, hpix, ascentpix, cg, selected, offset, fmt, font, textColor, drawingShadow );
01726 }
01727 
01728 
01729 /******************************************************************/
01730 /* Class: KoNoteVariable                                          */
01731 /******************************************************************/
01732 KoNoteVariable::KoNoteVariable( KoTextDocument *textdoc, const QString & _note,KoVariableFormat *varFormat,KoVariableCollection *_varColl )
01733     : KoVariable( textdoc, varFormat,_varColl )
01734 {
01735     m_varValue = QVariant( _note );
01736 }
01737 
01738 QString KoNoteVariable::fieldCode()
01739 {
01740     return i18n("Note");
01741 }
01742 
01743 void KoNoteVariable::saveVariable( QDomElement& parentElem )
01744 {
01745     QDomElement linkElem = parentElem.ownerDocument().createElement( "NOTE" );
01746     parentElem.appendChild( linkElem );
01747     linkElem.setAttribute( "note", m_varValue.toString() );
01748 }
01749 
01750 void KoNoteVariable::load( QDomElement& elem )
01751 {
01752     KoVariable::load( elem );
01753     QDomElement linkElem = elem.namedItem( "NOTE" ).toElement();
01754     if (!linkElem.isNull())
01755     {
01756         m_varValue = QVariant(linkElem.attribute("note"));
01757     }
01758 }
01759 
01760 void KoNoteVariable::recalc()
01761 {
01762     resize();
01763 }
01764 
01765 QStringList KoNoteVariable::actionTexts()
01766 {
01767     return QStringList( i18n( "Note..." ) );
01768 }
01769 
01770 QString KoNoteVariable::text(bool realValue)
01771 {
01772     if (m_varColl->variableSetting()->displayComment() &&
01773         m_varColl->variableSetting()->displayFieldCode()&&!realValue)
01774         return fieldCode();
01775     else
01776         //for a note return just a "space" we can look at
01777         //note when we "right button"
01778         return QString(" ");
01779 
01780 }
01781 
01782 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 )
01783 {
01784     if ( !m_varColl->variableSetting()->displayComment())
01785         return;
01786 
01787     KoTextFormat * fmt = format();
01788     //kdDebug(32500) << "KoNoteVariable::drawCustomItem index=" << index() << " x=" << x << " y=" << y << endl;
01789 
01790     p->save();
01791     p->setPen( QPen( fmt->color() ) );
01792     if ( fmt->textBackgroundColor().isValid() )
01793         p->fillRect( x, y, wpix, hpix, fmt->textBackgroundColor() );
01794     if ( selected )
01795     {
01796         p->setPen( QPen( cg.color( QColorGroup::HighlightedText ) ) );
01797         p->fillRect( x, y, wpix, hpix, cg.color( QColorGroup::Highlight ) );
01798     }
01799     else if ( textDocument() && p->device()->devType() != QInternal::Printer
01800         && !textDocument()->dontDrawingNoteVariable())
01801     {
01802         p->fillRect( x, y, wpix, hpix, Qt::yellow);
01803         p->setPen( QPen( cg.color( QColorGroup::Highlight ), 0, Qt::DotLine ) );
01804         p->drawRect( x, y, wpix, hpix );
01805     }
01806     //call it for use drawCustomItemHelper just for draw font effect
01807     KoVariable::drawCustomItem( p, x, y, wpix, hpix, ascentpix, cx, cy, cw, ch, cg, selected, offset, drawingShadow );
01808 
01809     p->restore();
01810 }
01811 
01812 void KoPgNumVariable::setSectionTitle( const QString& _title )
01813 {
01814     QString title( _title );
01815     if ( title.isEmpty() )
01816     {
01817         title = i18n("<None>"); // TODO after msg freeze: <No title>
01818     }
01819     m_varValue = QVariant( title );
01820 }
01821 
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 Wed Nov 17 13:19:27 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2003