Cppcheck
fileviewdialog.cpp
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2023 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "fileviewdialog.h"
20 
21 #include <QByteArray>
22 #include <QDialogButtonBox>
23 #include <QFile>
24 #include <QIODevice>
25 #include <QMessageBox>
26 #include <QTextEdit>
27 
28 #include "ui_fileview.h"
29 
30 FileViewDialog::FileViewDialog(const QString &file,
31  const QString &title,
32  QWidget *parent)
33  : QDialog(parent)
34  , mUI(new Ui::Fileview)
35 {
36  mUI->setupUi(this);
37 
38 
39  setWindowTitle(title);
40  connect(mUI->mButtons, SIGNAL(accepted()), this, SLOT(accept()));
41  loadTextFile(file, mUI->mText);
42 }
43 
45 {
46  delete mUI;
47 }
48 
49 void FileViewDialog::loadTextFile(const QString &filename, QTextEdit *edit)
50 {
51  QFile file(filename);
52  if (!file.exists()) {
53  QString msg(tr("Could not find the file: %1"));
54  msg = msg.arg(filename);
55 
56  QMessageBox msgbox(QMessageBox::Critical,
57  tr("Cppcheck"),
58  msg,
59  QMessageBox::Ok,
60  this);
61  msgbox.exec();
62  return;
63  }
64 
65  file.open(QIODevice::ReadOnly | QIODevice::Text);
66  if (!file.isReadable()) {
67  QString msg(tr("Could not read the file: %1"));
68  msg = msg.arg(filename);
69 
70  QMessageBox msgbox(QMessageBox::Critical,
71  tr("Cppcheck"),
72  msg,
73  QMessageBox::Ok,
74  this);
75  msgbox.exec();
76  return;
77  }
78  QByteArray filedata = file.readAll();
79  file.close();
80 
81  edit->setPlainText(filedata);
82 }
~FileViewDialog() override
void loadTextFile(const QString &filename, QTextEdit *edit)
Load text file contents to edit control.
Ui::Fileview * mUI
FileViewDialog(const QString &file, const QString &title, QWidget *parent=nullptr)
Definition: aboutdialog.h:27