File indexing completed on 2024-05-05 17:15:14

0001 /**********************************************************************************
0002     begin                : Tuesday Nov 1 2005
0003     copyright            : (C) 2005 by Thomas Braun (thomas.braun@virtuell-zuhause.de)
0004                                2015 by Andreas Cord-Landwehr (cordlandwehr@kde.org)
0005  **********************************************************************************/
0006 
0007 /***************************************************************************
0008  *                                                                         *
0009  *   This program is free software; you can redistribute it and/or modify  *
0010  *   it under the terms of the GNU General Public License as published by  *
0011  *   the Free Software Foundation; either version 2 of the License, or     *
0012  *   (at your option) any later version.                                   *
0013  *                                                                         *
0014  ***************************************************************************/
0015 
0016 #include "dialogs/statisticsdialog.h"
0017 #include "kileproject.h"
0018 #include "widgets/statisticswidget.h"
0019 
0020 #include <KConfigGroup>
0021 #include <KHelpClient>
0022 #include <KLocalizedString>
0023 #include <KTextEditor/Application>
0024 #include <KTextEditor/MainWindow>
0025 #include <KTextEditor/View>
0026 #include <QApplication>
0027 #include <QClipboard>
0028 #include <QDialogButtonBox>
0029 #include <QLabel>
0030 #include <QPushButton>
0031 #include <QVBoxLayout>
0032 
0033 // A dialog that displays statistical information about the active project/file
0034 
0035 namespace KileDialog {
0036 
0037 StatisticsDialog::StatisticsDialog(KileProject *project, KileDocument::TextInfo* docinfo, QWidget* parent,
0038                                    KTextEditor::View *view, const QString &caption)
0039     : KPageDialog(parent), m_project(project), m_docinfo(docinfo), m_view(view)
0040 {
0041     setFaceType(Tabbed);
0042     setWindowTitle(caption);
0043     setModal(true);
0044     setStandardButtons(QDialogButtonBox::Help | QDialogButtonBox::Close);
0045     QWidget *mainWidget = new QWidget(this);
0046     QVBoxLayout *mainLayout = new QVBoxLayout;
0047     setLayout(mainLayout);
0048     mainLayout->addWidget(mainWidget);
0049     QPushButton *copyButton = new QPushButton;
0050     copyButton->setText(i18n("Copy"));
0051     buttonBox()->addButton(copyButton, QDialogButtonBox::ActionRole);
0052     QPushButton *copyLatexButton = new QPushButton;
0053     copyLatexButton->setText(i18n("Copy as LaTeX"));
0054     buttonBox()->addButton(copyLatexButton, QDialogButtonBox::ActionRole);
0055     buttonBox()->button(QDialogButtonBox::Close)->setDefault(true);
0056 
0057     connect(copyButton, &QPushButton::clicked, this, [=]() {
0058         KILE_DEBUG_MAIN << "Open tab is" << currentPage()->name() << ' ' + (m_pagetoname.contains(currentPage()) ?  m_pagetoname[currentPage()] : "No such entry");
0059         QClipboard *clip = QApplication::clipboard();
0060         QString text;
0061         convertText(&text, false);
0062         clip->setText(text, QClipboard::Selection); // the text will be available with the middle mouse button
0063     });
0064     connect(copyLatexButton, &QPushButton::clicked, this, [=]() {
0065         KILE_DEBUG_MAIN << "Open tab is" << currentPage()->name() << ' ' + (m_pagetoname.contains(currentPage()) ?  m_pagetoname[currentPage()] : "No such entry");
0066         QClipboard *clip = QApplication::clipboard();
0067         QString text;
0068         convertText(&text, true);
0069         clip->setText(text, QClipboard::Selection); // the text will be available with the middle mouse button
0070     });
0071     connect(buttonBox(), &QDialogButtonBox::helpRequested, this, [=] () {
0072         KHelpClient::invokeHelp("statistics", "kile");
0073     });
0074 
0075     m_summarystats = new long[SIZE_STAT_ARRAY];
0076     m_summarystats[0] = m_summarystats[1] = m_summarystats[2] = m_summarystats[3] = m_summarystats[4] = m_summarystats[5] = 0;
0077 
0078     const long* stats;
0079     QString tempName;
0080     KileWidget::StatisticsWidget* tempWidget;
0081     KileWidget::StatisticsWidget* summary;
0082     KileDocument::TextInfo* tempDocinfo;
0083 
0084     m_hasSelection = view->selection(); // class variable, if the user has selected text,
0085     summary = new KileWidget::StatisticsWidget(mainWidget);
0086     KPageWidgetItem *itemSummary = new KPageWidgetItem(summary, i18n("Summary"));
0087     addPage(itemSummary);
0088     summary->m_commentAboutHelp->setText(i18n("For information about the accuracy see the Help."));
0089 
0090     // we have in every case a summary tab
0091     m_pagetowidget[itemSummary] = summary;
0092     m_pagetoname[itemSummary] = i18n("Summary");
0093 
0094     if (!m_project) { // the active doc doesn't belong to a project
0095         setWindowTitle(i18n("Statistics for %1", m_docinfo->getDoc()->url().fileName()));
0096         stats = m_docinfo->getStatistics(m_view);
0097         fillWidget(stats, summary);
0098     }
0099     else { // active doc belongs to a project
0100         setWindowTitle(i18n("Statistics for the Project %1", m_project->name()));
0101         KILE_DEBUG_MAIN << "Project file is " << project->baseURL() << Qt::endl;
0102 
0103         QList<KileProjectItem*> items = project->items();
0104 
0105         if (m_hasSelection) { // if the active doc has a selection
0106             stats = m_docinfo->getStatistics(m_view);
0107             fillWidget(stats, summary); // if yes we fill the summary widget and are finished
0108         }
0109         else {
0110             for(QList<KileProjectItem*>::iterator i = items.begin(); i != items.end(); ++i) {
0111                 KileProjectItem *item = *i;
0112 
0113                 if (item->type() ==  KileProjectItem::ProjectFile) { // ignore project files
0114                     continue;
0115                 }
0116 
0117                 tempDocinfo = item->getInfo();
0118                 if(tempDocinfo && tempDocinfo->getDoc()) { // closed items don't have a doc
0119                     tempName = tempDocinfo->getDoc()->url().fileName();
0120                     stats = tempDocinfo->getStatistics(m_view);
0121 
0122                     for (uint j = 0; j < SIZE_STAT_ARRAY; j++) {
0123                         m_summarystats[j] += stats[j];
0124                     }
0125 
0126                     tempWidget = new KileWidget::StatisticsWidget();
0127                     KPageWidgetItem *itemTemp = new KPageWidgetItem(tempWidget, tempName);
0128                     addPage(itemTemp);
0129                     KILE_DEBUG_MAIN << "TempName is " << tempName << Qt::endl;
0130                     m_pagetowidget[itemTemp] = tempWidget;
0131                     m_pagetoname[itemTemp] = tempName;
0132                     fillWidget(stats, tempWidget);
0133                 }
0134                 else {
0135                     m_notAllFilesOpenWarning = true; // print warning
0136                 }
0137             }
0138 
0139             fillWidget(m_summarystats, summary);
0140             if (m_notAllFilesOpenWarning) {
0141                 summary->m_warning->setText(i18n("To get statistics for all project files, you have to open them all."));
0142             }
0143 
0144             KILE_DEBUG_MAIN << "All keys in name " << m_pagetoname.keys() << " Nr. of keys " << m_pagetowidget.count() << Qt::endl;
0145             KILE_DEBUG_MAIN << "All keys in widget " << m_pagetowidget.keys() << " Nr. of keys " << m_pagetowidget.count() << Qt::endl;
0146         }
0147     }
0148 }
0149 
0150 StatisticsDialog::~StatisticsDialog()
0151 {
0152     delete [] m_summarystats;
0153 }
0154 
0155 void StatisticsDialog::fillWidget(const long* stats, KileWidget::StatisticsWidget* widget)
0156 {
0157 // we don't have to write 0's in the number labels because this is the default value
0158     if (!stats || !widget) {
0159         return;
0160     }
0161 
0162     if (m_hasSelection) {
0163         widget->m_warning->setText(i18n("WARNING: These are the statistics for the selected text only."));
0164     }
0165 
0166     widget->m_wordChar->setText(QString::number(stats[0]));
0167     widget->m_commandChar->setText(QString::number(stats[1]));
0168     widget->m_whitespaceChar->setText(QString::number(stats[2]));
0169     widget->m_totalChar->setText(QString::number(stats[0] + stats[1] + stats[2]));
0170 
0171     widget->m_wordString->setText(QString::number(stats[3]));
0172     widget->m_commandString->setText(QString::number(stats[4]));
0173     widget->m_environmentString->setText(QString::number(stats[5]));
0174     widget->m_totalString->setText(QString::number(stats[3] + stats[4] + stats[5]));
0175 
0176     widget->updateColumns();
0177 }
0178 
0179 void StatisticsDialog::convertText(QString* text, bool forLaTeX) // the bool determines if we want plainText or LaTeXCode
0180 {
0181     KileWidget::StatisticsWidget* widget = m_pagetowidget[currentPage()];
0182     QString name = m_pagetoname[currentPage()];
0183     QString charGroupName = widget->m_charactersGroup->title();
0184     QString stringGroupName = widget->m_stringsGroup->title();
0185 
0186     if (forLaTeX) {
0187         text->append("\\begin{tabular}{ll}\n");
0188     }
0189 
0190     if (m_project && currentPage()) {
0191         text->append(i18n("Statistics for project %1, file %2", m_project->name(), name));
0192     }
0193     else {
0194         if (m_project) {
0195             text->append(i18n("Statistics for project %1", m_project->name()));
0196         }
0197         else {
0198             if (m_docinfo->getDoc()->url().isValid()) {
0199                 text->append(i18n("Statistics for %1", m_docinfo->getDoc()->url().fileName()));
0200             }
0201             else {
0202                 text->append(i18n("Statistics for Untitled"));
0203             }
0204         }
0205     }
0206     if(forLaTeX) {
0207         text->append(" & \\\\\\hline\n");
0208     }
0209     else {
0210         text->append("\n\n");
0211     }
0212     text->append(charGroupName + (forLaTeX ? " &  \\\\\n" : "\n"));
0213     text->append(widget->m_wordCharText->text() + (forLaTeX ? " & " : "\t") + widget->m_wordChar->text() + (forLaTeX ? " \\\\\n" : "\n"));
0214     text->append(widget->m_commandCharText->text() + (forLaTeX ? " & " : "\t") + widget->m_commandChar->text() + (forLaTeX ? " \\\\\n" : "\n"));
0215     text->append(widget->m_whitespaceCharText->text() + (forLaTeX ? " & " : "\t") + widget->m_whitespaceChar->text() + (forLaTeX ? " \\\\\n" : "\n"));
0216     text->append(widget->m_totalCharText->text() + (forLaTeX ? " & " : "\t") + widget->m_totalChar->text() + (forLaTeX ? " \\\\\n" : "\n"));
0217 
0218     text->append((forLaTeX ? " & \\\\\n" : "\n"));
0219     text->append(stringGroupName + (forLaTeX ? " &  \\\\\n" : "\n"));
0220     text->append(widget->m_wordStringText->text() + (forLaTeX ? " & " : "\t") + widget->m_wordString->text() + (forLaTeX ? " \\\\\n" : "\n"));
0221     text->append(widget->m_commandStringText->text() + (forLaTeX ? " & " : "\t") + widget->m_commandString->text() + (forLaTeX ? " \\\\\n" : "\n"));
0222     text->append(widget->m_environmentStringText->text() + (forLaTeX ? " & " : "\t") + widget->m_environmentString->text() + (forLaTeX ? " \\\\\n" : "\n"));
0223     text->append(widget->m_totalStringText->text() + (forLaTeX ? " & " : "\t") + widget->m_totalString->text() + (forLaTeX ? " \\\\\\hline\n" : "\n"));
0224 
0225     if (forLaTeX) {
0226         text->append("\\end{tabular}\n");
0227     }
0228 
0229     if (m_hasSelection) { // we can't have both cases
0230         text->append((forLaTeX ? "\\par\\bigskip\n" : "\n") + widget->m_warning->text() + '\n');
0231     }
0232     else {
0233         if(m_notAllFilesOpenWarning) {
0234             text->append((forLaTeX ? "\\par\\bigskip\n" : "\n") + widget->m_warning->text() + '\n');
0235         }
0236     }
0237 }
0238 
0239 }