Fix regressions
[kphotoalbum.git] / MainWindow / StatisticsDialog.cpp
blob2758f50994d7dddcac9907288f8e9abfb4aa6c84
1 #include "StatisticsDialog.h"
2 #include <QDebug>
3 #include <QComboBox>
4 #include <QGroupBox>
5 #include <QLabel>
6 #include <QFormLayout>
7 #include <QHeaderView>
8 #include "DB/ImageDB.h"
9 #include <klocale.h>
10 #include <QTreeWidget>
11 #include <QVBoxLayout>
12 #include "DB/Category.h"
13 #include "Utilities/ShowBusyCursor.h"
14 #include "DB/CategoryCollection.h"
15 #include "DB/ImageSearchInfo.h"
17 using namespace MainWindow;
19 StatisticsDialog::StatisticsDialog( QWidget* parent )
20 : KDialog( parent )
22 QWidget* top = new QWidget;
23 QVBoxLayout* layout = new QVBoxLayout( top );
24 setMainWidget(top);
26 QString txt = i18n("<h1>Description</h1>"
27 "<table>"
28 "<tr><td># of Items</td><td>This is the number of different items in the category</td></tr>"
29 "<tr><td>Tags Total</td><td>This is a count of how many tags was made,<br/>i.e. a simple counting though all the images</tr></tr>"
30 "<tr><td>Tags Per Picture</td><td>This tells you how many tags are on each picture on average</td></tr>"
31 "</table><br/><br/>"
32 "Don't get too attached to this dialog, it has the problem that it counts categories AND subcategories,<br/>"
33 "so if an image has been taken in Las Vegas, Nevada, USA, then 3 tags are counted for that image,<br/>"
34 "while it should only be one.</br>"
35 "I'm not really sure if it is worth fixing that bug (as it is pretty hard to fix),<br/>"
36 "so maybe the dialog will simply go away again");
38 QLabel* label = new QLabel(txt);
39 layout->addWidget( label );
41 layout->addWidget( createAnnotatedGroupBox() );
43 label = new QLabel(i18n("<h1>Statistics</h1>"));
44 layout->addWidget(label);
46 m_treeWidget = new QTreeWidget;
47 layout->addWidget( m_treeWidget );
49 QStringList labels;
50 labels << i18n("Category") << i18n("# of Items") << i18n("Tags Totals") << i18n("Tags Per Picture") << QString();
51 m_treeWidget->setHeaderLabels( labels );
54 void StatisticsDialog::show()
56 populate();
57 KDialog::show();
60 QSize MainWindow::StatisticsDialog::sizeHint() const
62 return QSize( 800, 800 );
65 QTreeWidgetItem* MainWindow::StatisticsDialog::addRow( const QString& title, int noOfTags, int tagCount, int imageCount, QTreeWidgetItem* parent )
67 QStringList list;
68 list << title
69 << QString::number(noOfTags)
70 << QString::number( tagCount )
71 << QString::number( (double) tagCount / imageCount, 'F', 2);
72 QTreeWidgetItem* item = new QTreeWidgetItem( parent, list );
73 for (int col =1;col <4; ++col )
74 item->setTextAlignment( col, Qt::AlignRight );
75 return item;
78 void MainWindow::StatisticsDialog::highlightTotalRow( QTreeWidgetItem* item )
80 for ( int col=0; col<5; ++col ) {
81 QFont font = item->data( col, Qt::FontRole ).value<QFont>();
82 font.setWeight( QFont::Bold );
83 item->setData( col, Qt::FontRole, font );
87 QGroupBox* MainWindow::StatisticsDialog::createAnnotatedGroupBox()
89 QGroupBox* box = new QGroupBox( i18n("Tag indication completed annotation") );
91 m_boxLayout = new QGridLayout(box);
92 m_boxLayout->setColumnStretch(2,1);
93 int row = -1;
95 QLabel* label = new QLabel(i18n("If you use a specific tag to indicate that an image has been tagged, then specify it here.") );
96 label->setWordWrap( true );
97 m_boxLayout->addWidget( label, ++row, 0, 1, 3 );
99 label = new QLabel( i18n("Category:") );
100 m_boxLayout->addWidget( label, ++row, 0 );
102 m_category = new QComboBox;
103 m_boxLayout->addWidget( m_category, row, 1 );
105 m_tagLabel = new QLabel(i18n("Tag:") );
106 m_boxLayout->addWidget( m_tagLabel, ++row, 0 );
108 m_tag = new QComboBox;
109 m_tag->setSizeAdjustPolicy(QComboBox::AdjustToContents);
110 m_boxLayout->addWidget( m_tag, row, 1 );
112 m_category->addItem( i18n("None") );
114 QList<DB::CategoryPtr> categories = DB::ImageDB::instance()->categoryCollection()->categories();
115 Q_FOREACH( const DB::CategoryPtr& category, categories ) {
116 if ( category->name() == QString::fromLatin1("Media Type") || category->name() == QString::fromLatin1("Folder"))
117 continue;
118 m_category->addItem( category->text(), category->name() );
121 connect( m_category, SIGNAL( activated(int) ), this, SLOT( categoryChanged(int) ) );
122 connect( m_tag, SIGNAL( activated(int) ), this, SLOT( populate() ) );
123 m_tagLabel->setEnabled(false);
124 m_tag->setEnabled(false);
126 return box;
129 void MainWindow::StatisticsDialog::categoryChanged(int index)
131 const bool enabled = (index != 0 );
132 m_tagLabel->setEnabled( enabled );
133 m_tag->setEnabled( enabled );
135 m_tag->clear();
137 if ( enabled ) {
138 const QString name = m_category->itemData(index).value<QString>();
139 DB::CategoryPtr category = DB::ImageDB::instance()->categoryCollection()->categoryForName( name );
140 m_tag->addItems( category->items() );
144 void MainWindow::StatisticsDialog::populate()
146 Utilities::ShowBusyCursor dummy;
147 m_treeWidget->clear();
149 const int imageCount = DB::ImageDB::instance()->totalCount();
150 QTreeWidgetItem* top = new QTreeWidgetItem( m_treeWidget, QStringList() << i18n("All") << QString::number(imageCount) );
151 top->setTextAlignment( 1, Qt::AlignRight );
152 populateSubTree( DB::ImageSearchInfo(), imageCount, top );
154 if ( m_category->currentIndex() != 0 ) {
155 const QString category = m_category->itemData(m_category->currentIndex()).value<QString>();
156 const QString tag = m_tag->currentText();
157 DB::ImageSearchInfo info;
158 info.setCategoryMatchText( category, tag );
159 const int imageCount = DB::ImageDB::instance()->count(info ).total();
160 QTreeWidgetItem* item = new QTreeWidgetItem( m_treeWidget,
161 QStringList() << QString::fromLatin1("%1: %2").arg(category).arg(tag)
162 << QString::number(imageCount));
163 item->setTextAlignment( 1, Qt::AlignRight );
164 populateSubTree( info, imageCount, item );
166 m_treeWidget->header()->resizeSections( QHeaderView::ResizeToContents );
169 void MainWindow::StatisticsDialog::populateSubTree( const DB::ImageSearchInfo& info, int imageCount, QTreeWidgetItem* top )
171 top->setExpanded(true);
173 QList<DB::CategoryPtr> categories = DB::ImageDB::instance()->categoryCollection()->categories();
175 int tagsTotal = 0;
176 int grantTotal = 0;
177 Q_FOREACH( const DB::CategoryPtr& category, categories ) {
178 if ( category->name() == QString::fromLatin1("Media Type") || category->name() == QString::fromLatin1("Folder"))
179 continue;
181 const QMap<QString,uint> tags = DB::ImageDB::instance()->classify( info, category->name(), DB::anyMediaType );
182 int total = 0;
183 for( QMap<QString,uint>::ConstIterator tagIt = tags.constBegin(); tagIt != tags.constEnd(); ++tagIt ) {
184 // Don't count the NONE tag, and the OK tag
185 if ( tagIt.key() != DB::ImageDB::NONE() && ( category->name() != m_category->currentText() || tagIt.key() != m_tag->currentText() ) )
186 total += tagIt.value();
190 addRow( category->text(), tags.count()-1, total, imageCount, top );
191 tagsTotal += tags.count() -1;
192 grantTotal += total;
195 QTreeWidgetItem* totalRow = addRow( i18n("Total"), tagsTotal, grantTotal, imageCount, top );
196 highlightTotalRow( totalRow );