Commit d195633b authored by Nate Graham's avatar Nate Graham 🔩
Browse files

Display all of the page sizes in a new tab of the properties dialog

Previously it just said, "Multiple" for multi-page-size documents.
Now it will tell you the most common page size on the "Properties"
tab, and display a full table of all page sizes in a new "Page Sizes" tab.

For documents with only a single page size, there are no changes.
parent d7a88a17
Pipeline #22316 passed with stage
in 12 minutes and 19 seconds
......@@ -166,8 +166,37 @@ QString DocumentPrivate::pagesSizeString() const
if (m_generator->pagesSizeMetric() != Generator::None)
{
QSizeF size = m_parent->allPagesSize();
// Single page size
if (size.isValid()) return localizedSize(size);
else return i18nc("Used in this context: 'Page sizes: Multiple'", "Multiple");
// Multiple page sizes
QString sizeString;
QHash<QString, int> pageSizeFrequencies;
// Compute frequencies of each page size
for (int i = 0; i < m_pagesVector.count(); ++i)
{
const Page *p = m_pagesVector.at(i);
sizeString = localizedSize( QSizeF( p->width(), p->height() ) );
pageSizeFrequencies[sizeString] = pageSizeFrequencies.value(sizeString, 0) + 1;
}
// Figure out which page size is most frequent
int largestFrequencySeen = 0;
QString mostCommonPageSize = QString();
QHash<QString, int>::const_iterator i = pageSizeFrequencies.constBegin();
while (i != pageSizeFrequencies.constEnd())
{
if (i.value() > largestFrequencySeen)
{
largestFrequencySeen = i.value();
mostCommonPageSize = i.key();
}
++i;
}
QString finalText = i18nc( "@info %1 is a page size", "Most pages are %1.", mostCommonPageSize );
return finalText;
}
else return QString();
}
......
......@@ -23,6 +23,7 @@
#include <QProgressBar>
#include <QPushButton>
#include <QSortFilterProxyModel>
#include <QTableView>
#include <QTreeView>
#include <QTimer>
......@@ -41,8 +42,8 @@ static const int FontInfoRole = Qt::UserRole + 1;
PropertiesDialog::PropertiesDialog(QWidget *parent, Okular::Document *doc)
: KPageDialog( parent ), m_document( doc ), m_fontPage( nullptr ),
m_fontModel( nullptr ), m_fontInfo( nullptr ), m_fontProgressBar( nullptr ),
m_fontScanStarted( false )
m_fontModel( nullptr ), m_pageSizesModel ( nullptr ), m_fontInfo( nullptr ),
m_fontProgressBar( nullptr ), m_fontScanStarted( false )
{
setFaceType( Tabbed );
setWindowTitle( i18n( "Unknown File" ) );
......@@ -154,6 +155,30 @@ PropertiesDialog::PropertiesDialog(QWidget *parent, Okular::Document *doc)
m_fontProgressBar->hide();
}
// PAGE SIZES
if ( !m_document->allPagesSize().isValid() ) {
// create page sizes tab tab and layout it when there are multiple page sizes
QFrame *page3 = new QFrame();
KPageWidgetItem *pageSizesPage = addPage( page3, i18n("&Page Sizes") );
pageSizesPage->setIcon( QIcon::fromTheme( QStringLiteral( "view-pages-overview" ) ) );
QVBoxLayout *page3Layout = new QVBoxLayout( page3 );
// Add a table view
QTableView *view = new QTableView( page3 );
m_pageSizesModel = new PageSizesModel( view, m_document );
page3Layout->addWidget( view );
view->setModel( m_pageSizesModel );
view->setAlternatingRowColors( true );
view->setCornerButtonEnabled( false );
view->resizeColumnsToContents();
view->verticalHeader()->hide();
// Stretch the last column, which is the widest
QHeaderView *headerView = view->horizontalHeader();
headerView->setSectionResizeMode(0, QHeaderView::Interactive);
headerView->setSectionResizeMode(1, QHeaderView::Stretch);
}
// KPageDialog is a bit buggy, it doesn't fix its own sizeHint, so we have to manually resize
resize(layout->sizeHint());
......@@ -424,6 +449,67 @@ int FontsListModel::rowCount( const QModelIndex &parent ) const
return parent.isValid() ? 0 : m_fonts.size();
}
PageSizesModel::PageSizesModel( QObject * parent, Okular::Document *doc )
: QAbstractTableModel( parent ), m_document ( doc )
{
}
PageSizesModel::~PageSizesModel()
{
}
int PageSizesModel::columnCount( const QModelIndex &parent ) const
{
return parent.isValid() ? 0 : 2;
}
QVariant PageSizesModel::data( const QModelIndex &index, int role ) const
{
if ( !index.isValid() || index.row() < 0 || index.row() >= (int) m_document->pages() )
return QVariant();
switch ( index.column() )
{
case 0:
{
if (role == Qt::DisplayRole) return index.row() + 1; // Page zero doesn't make sense to the user
else if (role == Qt::TextAlignmentRole) return Qt::AlignCenter;
break;
}
case 1:
if (role == Qt::DisplayRole) return m_document->pageSizeString( index.row() );
break;
}
return QVariant();
}
QVariant PageSizesModel::headerData( int section, Qt::Orientation orientation, int role ) const
{
if ( orientation != Qt::Horizontal )
return QVariant();
if ( role == Qt::TextAlignmentRole )
return QVariant( Qt::AlignLeft );
if ( role != Qt::DisplayRole )
return QVariant();
switch ( section )
{
case 0: return i18n( "Page" ); break;
case 1: return i18n( "Size" ); break;
default:
return QVariant();
}
}
int PageSizesModel::rowCount( const QModelIndex &parent ) const
{
return parent.isValid() ? 0 : m_document->pages();
}
#include "moc_propertiesdialog.cpp"
/* kate: replace-tabs on; indent-width 4; */
......@@ -20,6 +20,7 @@
class QLabel;
class QProgressBar;
class FontsListModel;
class PageSizesModel;
namespace Okular {
class Document;
......@@ -44,6 +45,7 @@ class PropertiesDialog : public KPageDialog
Okular::Document * m_document;
KPageWidgetItem * m_fontPage;
FontsListModel * m_fontModel;
PageSizesModel * m_pageSizesModel;
QLabel * m_fontInfo;
QProgressBar * m_fontProgressBar;
bool m_fontScanStarted;
......@@ -71,6 +73,25 @@ class FontsListModel
QList<Okular::FontInfo> m_fonts;
};
class PageSizesModel
: public QAbstractTableModel
{
Q_OBJECT
public:
explicit PageSizesModel( QObject * parent = nullptr, Okular::Document *doc = nullptr );
~PageSizesModel() override;
// reimplementations from QAbstractTableModel
int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
private:
Okular::Document * m_document;
};
#endif
/* kate: replace-tabs on; indent-width 4; */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment