From b9d749bc0dada4bbafba730f40cd8931cbe42ec5 Mon Sep 17 00:00:00 2001 From: Albert Astals Cid Date: Mon, 16 Nov 2009 00:46:33 +0000 Subject: [PATCH] Commit "Document information" patch by Luigi Toscano, 8 months without a nack from any devel and i like it, so let's commit it Luigi's mail says ******** The attached patch changes a bit the handling of document informations and the informations shown in the info page. - document path is added to the information and a button allows you to toggle between to file name (default) and the complete path; - the number of the page is added to the information set by documentInfo() and not by the properties dialog (so the latter will not modify datas); - the order of the information shown is the fixed now (see orderedProperties). ********* Then there are a few things of my own since some const here and there and adding three values to Key enum Also it fixes wish 208999 BUGS: 208999 svn path=/trunk/KDE/kdegraphics/okular/; revision=1049872 --- core/document.cpp | 158 +++++++++++++++++++++++++++++++--------- core/document.h | 22 +++++- core/document_p.h | 4 +- ui/propertiesdialog.cpp | 105 ++++++++++++++++++++------ ui/propertiesdialog.h | 8 ++ 5 files changed, 239 insertions(+), 58 deletions(-) diff --git a/core/document.cpp b/core/document.cpp index e863ce366..c2f7aef61 100644 --- a/core/document.cpp +++ b/core/document.cpp @@ -1868,6 +1868,10 @@ void Document::closeDocument() d->m_allocatedTextPagesFifo.clear(); d->m_pageSize = PageSize(); d->m_pageSizes.clear(); + + delete d->m_documentInfo; + d->m_documentInfo = 0; + AudioPlayer::instance()->d->m_currentDocument = KUrl(); } @@ -1975,23 +1979,37 @@ bool Document::canConfigurePrinter( ) const const DocumentInfo * Document::documentInfo() const { + if ( d->m_documentInfo ) + return d->m_documentInfo; + if ( d->m_generator ) { - const DocumentInfo *infoConst = d->m_generator->generateDocumentInfo(); - if ( !infoConst ) - return 0; + DocumentInfo *info = new DocumentInfo(); + const DocumentInfo *tmp = d->m_generator->generateDocumentInfo(); + if ( tmp ) + *info = *tmp; - DocumentInfo *info = const_cast< DocumentInfo * >( infoConst ); - QString pagesSize = d->pagesSizeString(); + info->set( DocumentInfo::FilePath, currentDocument().url() ); + const QString pagesSize = d->pagesSizeString(); if ( d->m_docSize != -1 ) { - QString sizeString = KGlobal::locale()->formatByteSize( d->m_docSize ); - info->set( "documentSize", sizeString, i18n( "File Size" ) ); + const QString sizeString = KGlobal::locale()->formatByteSize( d->m_docSize ); + info->set( DocumentInfo::DocumentSize, sizeString ); } if (!pagesSize.isEmpty()) { - info->set( "pagesSize", pagesSize, i18n("Page Size") ); + info->set( DocumentInfo::PagesSize, pagesSize ); + } + + const DocumentInfo::Key keyPages = DocumentInfo::Pages; + const QString keyString = DocumentInfo::getKeyString( keyPages ); + + if ( info->get( keyString ).isEmpty() ) { + info->set( keyString, QString::number( this->pages() ), + DocumentInfo::getKeyTitle( keyPages ) ); } + + d->m_documentInfo = info; return info; } else return NULL; @@ -3729,65 +3747,139 @@ void DocumentInfo::set( const QString &key, const QString &value, docElement.appendChild( element ); } -void DocumentInfo::set( enum Key key, const QString &value ) +void DocumentInfo::set( Key key, const QString &value ) +{ + const QString keyString = getKeyString( key ); + if ( !keyString.isEmpty() ) + set( keyString, value, getKeyTitle( key ) ); + else + kWarning(OkularDebug) << "Invalid key passed"; +} + +QString DocumentInfo::get( const QString &key ) const +{ + const QDomElement docElement = documentElement(); + + // check whether key already exists + const QDomNodeList list = docElement.elementsByTagName( key ); + if ( list.count() > 0 ) + return list.item( 0 ).toElement().attribute( "value" ); + else + return QString(); +} + +QString DocumentInfo::getKeyString( Key key ) //const { switch ( key ) { case Title: - set( "title", value, i18n( "Title" ) ); + return "title"; break; case Subject: - set( "subject", value, i18n( "Subject" ) ); + return "subject"; break; case Description: - set( "description", value, i18n( "Description" ) ); + return "description"; break; case Author: - set( "author", value, i18n( "Author" ) ); + return "author"; break; case Creator: - set( "creator", value, i18n( "Creator" ) ); + return "creator"; break; case Producer: - set( "producer", value, i18n( "Producer" ) ); + return "producer"; break; case Copyright: - set( "copyright", value, i18n( "Copyright" ) ); + return "copyright"; break; case Pages: - set( "pages", value, i18n( "Pages" ) ); + return "pages"; break; case CreationDate: - set( "creationDate", value, i18n( "Created" ) ); + return "creationDate"; break; case ModificationDate: - set( "modificationDate", value, i18n( "Modified" ) ); + return "modificationDate"; break; case MimeType: - set( "mimeType", value, i18n( "Mime Type" ) ); + return "mimeType"; break; case Category: - set( "category", value, i18n( "Category" ) ); + return "category"; break; case Keywords: - set( "keywords", value, i18n( "Keywords" ) ); + return "keywords"; + break; + case FilePath: + return "filePath"; + break; + case DocumentSize: + return "documentSize"; + break; + case PagesSize: + return "pageSize"; break; default: - kWarning(OkularDebug) << "Invalid key passed"; + return QString(); break; } } -QString DocumentInfo::get( const QString &key ) const +QString DocumentInfo::getKeyTitle( Key key ) //const { - QDomElement docElement = documentElement(); - QDomElement element; - - // check whether key already exists - QDomNodeList list = docElement.elementsByTagName( key ); - if ( list.count() > 0 ) - return list.item( 0 ).toElement().attribute( "value" ); - else - return QString(); + switch ( key ) { + case Title: + return i18n( "Title" ); + break; + case Subject: + return i18n( "Subject" ); + break; + case Description: + return i18n( "Description" ); + break; + case Author: + return i18n( "Author" ); + break; + case Creator: + return i18n( "Creator" ); + break; + case Producer: + return i18n( "Producer" ); + break; + case Copyright: + return i18n( "Copyright" ); + break; + case Pages: + return i18n( "Pages" ); + break; + case CreationDate: + return i18n( "Created" ); + break; + case ModificationDate: + return i18n( "Modified" ); + break; + case MimeType: + return i18n( "Mime Type" ); + break; + case Category: + return i18n( "Category" ); + break; + case Keywords: + return i18n( "Keywords" ); + break; + case FilePath: + return i18n( "File Path" ); + break; + case DocumentSize: + return i18n( "File Size" ); + break; + case PagesSize: + return i18n("Page Size"); + break; + default: + return QString(); + break; + } } diff --git a/core/document.h b/core/document.h index 9e20e9538..bb631a1ef 100644 --- a/core/document.h +++ b/core/document.h @@ -829,7 +829,10 @@ class OKULAR_EXPORT DocumentInfo : public QDomDocument ModificationDate, ///< The date of last modification of the document MimeType, ///< The mime type of the document Category, ///< The category of the document - Keywords ///< The keywords which describe the content of the document + Keywords, ///< The keywords which describe the content of the document + FilePath, ///< The path of the file @since 0.10 (KDE 4.4) + DocumentSize, ///< The size of the document @since 0.10 (KDE 4.4) + PagesSize ///< The size of the pages (if all pages have the same size) @since 0.10 (KDE 4.4) }; /** @@ -848,13 +851,26 @@ class OKULAR_EXPORT DocumentInfo : public QDomDocument * Sets the value for a predefined key. You should use this method * whenever a predefined key exists for your value. */ - void set( enum Key, const QString &value ); + void set( Key key, const QString &value ); /** * Returns the value for a given key or an empty string when the * key doesn't exist. */ QString get( const QString &key ) const; + + /** + * Returns the internal string for the given key + * @since 0.10 (KDE 4.4) + */ + static QString getKeyString( Key key ); + + /** + * Returns the user visible string for the given key + * @since 0.10 (KDE 4.4) + */ + static QString getKeyTitle( Key key ); + }; /** @@ -981,3 +997,5 @@ Q_DECLARE_METATYPE( Okular::DocumentInfo::Key ) Q_DECLARE_OPERATORS_FOR_FLAGS( Okular::Document::PixmapRequestFlags ) #endif + +/* kate: replace-tabs on; indent-width 4; */ diff --git a/core/document_p.h b/core/document_p.h index 5c9493a55..96cc4d60d 100644 --- a/core/document_p.h +++ b/core/document_p.h @@ -84,7 +84,8 @@ class DocumentPrivate m_closingLoop( 0 ), m_scripter( 0 ), m_archiveData( 0 ), - m_fontsCached( false ) + m_fontsCached( false ), + m_documentInfo( 0 ) { calculateMaxTextPages(); } @@ -220,6 +221,7 @@ class DocumentPrivate QPointer< FontExtractionThread > m_fontThread; bool m_fontsCached; + DocumentInfo *m_documentInfo; FontInfo::List m_fontsCache; QSet< View * > m_views; diff --git a/ui/propertiesdialog.cpp b/ui/propertiesdialog.cpp index 000ab1da1..ebe2f0d7d 100644 --- a/ui/propertiesdialog.cpp +++ b/ui/propertiesdialog.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -25,6 +26,7 @@ #include #include #include +#include // local includes #include "core/document.h" @@ -57,6 +59,8 @@ PropertiesDialog::PropertiesDialog(QWidget *parent, Okular::Document *doc) return; } + m_showingTitle = true; + // mime name based on mimetype id QString mimeName = info->get( "mimeType" ).section( '/', -1 ).toUpper(); setCaption( i18n( "%1 Properties", mimeName ) ); @@ -65,35 +69,69 @@ PropertiesDialog::PropertiesDialog(QWidget *parent, Okular::Document *doc) int row = 0; int valMaxWidth = 100; + + const QString filePathKey = Okular::DocumentInfo::getKeyString( Okular::DocumentInfo::FilePath ); + + /* obtains the properties list, conveniently ordered */ + QStringList orderedProperties; + orderedProperties << filePathKey + << Okular::DocumentInfo::getKeyString( Okular::DocumentInfo::PagesSize ) + << Okular::DocumentInfo::getKeyString( Okular::DocumentInfo::DocumentSize ); + for (Okular::DocumentInfo::Key ks = Okular::DocumentInfo::Title; + ks <= Okular::DocumentInfo::Keywords; + ks = Okular::DocumentInfo::Key( ks+1 ) ) { + orderedProperties << Okular::DocumentInfo::getKeyString( ks ); + } for ( QDomNode node = docElement.firstChild(); !node.isNull(); node = node.nextSibling() ) { - QDomElement element = node.toElement(); + const QDomElement element = node.toElement(); - QString titleString = element.attribute( "title" ); - QString valueString = element.attribute( "value" ); + const QString titleString = element.attribute( "title" ); + const QString valueString = element.attribute( "value" ); if ( titleString.isEmpty() || valueString.isEmpty() ) continue; - - // create labels and layout them - QLabel *key = new QLabel( i18n( "%1:", titleString ), page ); - QLabel *value = new KSqueezedTextLabel( valueString, page ); - value->setTextInteractionFlags( Qt::TextSelectableByMouse ); - layout->addWidget( key, row, 0, Qt::AlignRight ); - layout->addWidget( value, row, 1 ); - row++; - - // refine maximum width of 'value' labels - valMaxWidth = qMax( valMaxWidth, fontMetrics().width( valueString ) ); + if ( !orderedProperties.contains( titleString ) ) + orderedProperties << titleString; } - // add the number of pages if the generator hasn't done it already - QDomNodeList list = docElement.elementsByTagName( "pages" ); - if ( list.count() == 0 ) { - QLabel *key = new QLabel( i18n( "Pages:" ), page ); - QLabel *value = new QLabel( QString::number( doc->pages() ), page ); - value->setTextInteractionFlags( Qt::TextSelectableByMouse ); + QDomNodeList list; + + for ( QStringList::Iterator it = orderedProperties.begin(); + it != orderedProperties.end(); ++it ) { + list = docElement.elementsByTagName( (*it) ); + if ( list.count() == 1 ) { + + QDomElement element = list.at(0).toElement(); + const QString titleString = element.attribute( "title" ); + const QString valueString = element.attribute( "value" ); + if ( titleString.isEmpty() || valueString.isEmpty() ) + continue; + + // create labels and layout them + KSqueezedTextLabel *value = new KSqueezedTextLabel( valueString, page ); + QWidget *key; + if ( element.tagName() == filePathKey ) { + m_toggleTitlePath = new QPushButton( page ); + setToggleTitlePathText(); + + m_toggleTitlePath->setFlat( true ); + connect( m_toggleTitlePath, SIGNAL( clicked() ), this, SLOT( filePathViewChanged() ) ); + key = m_toggleTitlePath; + m_filePathNameLabel = value; + m_filePathName = valueString; + + KUrl vurl( valueString ); + value->setText( vurl.fileName() ); + } else { + key = new QLabel( i18n( "%1:", titleString ), page ); + } + value->setTextInteractionFlags( Qt::TextSelectableByMouse ); + layout->addWidget( key, row, 0, Qt::AlignRight ); + layout->addWidget( value, row, 1 ); + row++; - layout->addWidget( key, row, 0, Qt::AlignRight ); - layout->addWidget( value, row, 1 ); + // refine maximum width of 'value' labels + valMaxWidth = qMax( valMaxWidth, fontMetrics().width( valueString ) ); + } } // FONTS @@ -166,6 +204,29 @@ void PropertiesDialog::pageChanged( KPageWidgetItem *current, KPageWidgetItem * } } +void PropertiesDialog::setToggleTitlePathText() +{ + QString text; + if (m_showingTitle) + { + text = i18nc( "%1 is \"Title\"", "%1:", Okular::DocumentInfo::getKeyTitle( Okular::DocumentInfo::Title ) ); + } + else + { + text = i18nc( "%1 is \"File Path\"", "%1:", Okular::DocumentInfo::getKeyTitle( Okular::DocumentInfo::FilePath ) ); + } + m_toggleTitlePath->setText( text ); +} + +void PropertiesDialog::filePathViewChanged() +{ + m_showingTitle = !m_showingTitle; + setToggleTitlePathText(); + QString tmp(m_filePathName); + m_filePathName = m_filePathNameLabel->text(); + m_filePathNameLabel->setText( tmp ); +} + void PropertiesDialog::slotFontReadingProgress( int page ) { m_fontProgressBar->setValue( m_fontProgressBar->maximum() * ( page + 1 ) / m_document->pages() ); diff --git a/ui/propertiesdialog.h b/ui/propertiesdialog.h index 59ff2180f..6790993cc 100644 --- a/ui/propertiesdialog.h +++ b/ui/propertiesdialog.h @@ -20,6 +20,7 @@ class QLabel; class QProgressBar; class FontsListModel; +class KSqueezedTextLabel; namespace Okular { class Document; @@ -35,18 +36,25 @@ class PropertiesDialog : public KPageDialog private slots: void pageChanged( KPageWidgetItem *, KPageWidgetItem * ); + void filePathViewChanged(); void slotFontReadingProgress( int page ); void slotFontReadingEnded(); void reallyStartFontReading(); void showFontsMenu(const QPoint &pos); private: + void setToggleTitlePathText(); + Okular::Document * m_document; KPageWidgetItem * m_fontPage; FontsListModel * m_fontModel; QLabel * m_fontInfo; QProgressBar * m_fontProgressBar; bool m_fontScanStarted; + bool m_showingTitle; + QString m_filePathName; + KSqueezedTextLabel *m_filePathNameLabel; + QPushButton *m_toggleTitlePath; }; class FontsListModel