First SVN commit
[fapman] / logview.cpp
1 /*
2         This file is part of Faster Application Manager.
3
4         Faster Application Manager is free software: you can redistribute it and/or modify
5         it under the terms of the GNU General Public License as published by
6         the Free Software Foundation, either version 3 of the License, or
7         (at your option) any later version.
8
9         Faster Application Manager is distributed in the hope that it will be useful,
10         but WITHOUT ANY WARRANTY; without even the implied warranty of
11         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12         GNU General Public License for more details.
13
14         You should have received a copy of the GNU General Public License
15         along with Faster Application Manager.  If not, see <http://www.gnu.org/licenses/>.
16
17         (C) Heikki Holstila 2010
18 */
19
20 #include <QtGui>
21
22 #ifdef Q_WS_MAEMO_5
23 #include <QtMaemo5>
24 #endif
25
26 #include "logview.h"
27 #include "ui_logview.h"
28 #include "aaptinterface.h"
29
30 LogView::LogView(QByteArray text, QWidget *parent) :
31     QDialog(parent),
32     ui(new Ui::LogView)
33 {
34     ui->setupUi(this);
35
36         QFont f = ui->plainTextEdit->font();
37         f.setPointSize( f.pointSize()-2 );
38
39         ui->plainTextEdit->setFont(f);
40
41         QString t = text;
42         QStringList lines = t.split('\n');
43
44         if( lines.count()>100 )
45                 ui->label->setText("Showing last 100 lines");
46
47         while( lines.count()>100 )
48                 lines.removeFirst();
49         lines << "" << "" << "" << "" << "";
50
51         ui->plainTextEdit->setPlainText("");
52         ui->plainTextEdit->appendPlainText(lines.join("\n"));
53 }
54
55 LogView::~LogView()
56 {
57     delete ui;
58 }
59
60 int LogView::exec()
61 {
62 #ifdef Q_WS_MAEMO_5             // workaround for buggy Qt autorotation
63         QRect r = dynamic_cast<QWidget*>(this->parent())->rect();
64         if(r.width() < r.height())
65                 this->setAttribute(Qt::WA_Maemo5PortraitOrientation);
66 #endif
67         show();
68
69         //ui->plainTextEdit->setMinimumHeight( ui->plainTextEdit->contentsRect().height() );
70         //ui->scrollAreaWidgetContents->setMinimumHeight( ui->scrollAreaWidgetContents->contentsRect().height() );
71         //ui->plainTextEdit->setMinimumHeight( 800 );
72         //ui->plainTextEdit->adjustSize();
73         //ui->scrollAreaWidgetContents->adjustSize();
74
75         // taken from a Qt example, should auto-resize the textedit
76         QTextDocument *doc = ui->plainTextEdit->document();
77         QSize s = doc->size().toSize();
78         if (ui->plainTextEdit)
79                 s.setHeight((s.height() + 1) * ui->plainTextEdit->fontMetrics().lineSpacing());
80         const QRect fr = ui->plainTextEdit->frameRect();
81         const QRect cr = ui->plainTextEdit->contentsRect();
82         ui->plainTextEdit->setMinimumHeight(qMax(70, s.height() + (fr.height() - cr.height() - 1)));
83
84         int e = QDialog::exec();
85
86 #ifdef Q_WS_MAEMO_5             // workaround for buggy Qt autorotation
87         this->setAttribute(Qt::WA_Maemo5AutoOrientation);
88 #endif
89         return e;
90 }
91
92 void LogView::changeEvent(QEvent *e)
93 {
94     QDialog::changeEvent(e);
95     switch (e->type()) {
96     case QEvent::LanguageChange:
97         ui->retranslateUi(this);
98         break;
99     default:
100         break;
101     }
102 }
103
104 void LogView::on_btnClear_clicked()
105 {
106         QFile logfile(KLogFile);
107         logfile.remove();
108
109         accept();
110 }
111
112 void LogView::on_btnCopy_clicked()
113 {
114         ui->plainTextEdit->selectAll();
115         ui->plainTextEdit->copy();
116
117         QMaemo5InformationBox::information ( this, "Copied" );
118 }