-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviewer_mainwin.cpp
144 lines (143 loc) · 4.94 KB
/
viewer_mainwin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "viewer_mainwin.h"
#include "ui_viewer_mainwin.h"
#include <QIcon>
#include <QFileDialog>
#include <QDebug>
#include <QDir>
#include <QTextStream>
#include <QPrintDialog>
#include <QMessageBox>
#include <QMimeData>
#include "defines.h"
QString const extern APP_NAME;
viewer_mainwin::viewer_mainwin(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, trdlg(new GTranslateDialog)
{
ui->setupUi(this);
setAcceptDrops(true);
is_opened = false;
is_empty = false;
actionSetup();
iconSetup();
setCentralWidget(ui->textEdit);
currentPath = QDir::homePath();
ui->actionAbout->setText(tr("&About %1").arg(APP_NAME));
}
viewer_mainwin::~viewer_mainwin()
{
delete trdlg;
delete ui;
}
void viewer_mainwin::actionSetup(){
qDebug() << "MSG:" << tr("Connecting actions...");
connect(ui->actionOpen_O,SIGNAL(triggered()),this,SLOT(fileOpen()));
connect(ui->action_Print,SIGNAL(triggered()),this,SLOT(filePrint()));
connect(ui->action_Translate,SIGNAL(triggered()),this,SLOT(trDlgOpen()));
connect(ui->actionAbout,SIGNAL(triggered()),this,SLOT(showabout()));
connect(ui->actionExit,SIGNAL(triggered()),this,SLOT(close()));
connect(ui->textEdit,SIGNAL(textChanged()),this,SLOT(setData()));
qDebug() << "MSG:" << tr("Done");
this->setContextMenuPolicy(Qt::NoContextMenu);
about_=new AboutDialog();
ui->action_Print->setEnabled(is_opened);
}
void viewer_mainwin::iconSetup(){
qDebug() << "MSG:" << tr("Setting up icons");
ui->actionOpen_O->setIcon(ICON_OPEN);
ui->action_Print->setIcon(ICON_PRINT);
ui->actionExit->setIcon(ICON_EXIT);
ui->action_Translate->setIcon(ICON_TRANSLATE);
qDebug() << "MSG:" << tr("Done");
}
void viewer_mainwin::fileOpen(const QString &fname){
QString name;
if(fname.isEmpty())
{
QFileDialog filed;
name = filed.getOpenFileName(this,tr("Open file"),currentPath,tr("Text files(*.txt);;"
"HTML document(*.html *.htm);;"
"Markdown document(*.md *.markdown);;"
"C/C++ Source code and Header(*.c *.cpp *.cc *.h *.cxx);;"
"JavaScript(*.js);;"
"All files(*.* *)"));
}else{
name = fname;
}
if (name.isEmpty()){
return;
}
QString content=loadFile(name);
ui->textEdit->setPlainText(content);
QFileInfo forpath(name);
currentPath = forpath.absolutePath();
}
void viewer_mainwin::filePrint(){
QPrintDialog pdlg;
if(is_empty){
if(QMessageBox::question(this,tr("Question"),tr("This file is empty.\nDo you want to continue?")) == QMessageBox::No) {
return;
}
}
qDebug() << "Execute the print dialog.";
int res=pdlg.exec();
if(res == QPrintDialog::Accepted){
qDebug() << "Accepted.Sent a job.";
ui->textEdit->print((QPagedPaintDevice*)pdlg.printer());
}
}
QString viewer_mainwin::loadFile(QString filename){
QFile qfilename(filename);
if (!qfilename.open(QIODevice::ReadOnly)) {
QString errMsg = qfilename.errorString();
QMessageBox::critical(this,tr("Error"),tr("Can't open %1:\n%2").arg(qfilename.fileName(),errMsg));
return tr("Error:\n%1").arg(errMsg);
}
QTextStream in(&qfilename);
QString line;
int lines;
lines=0;
while (!in.atEnd()){
lines++;
line += in.readLine().toUtf8()+"\n";
}
ui->statusbar->showMessage(tr("Opened \"%1\" :%2 lines").arg(filename,QString::number(lines)));
is_opened = true;
return line;
}
void viewer_mainwin::showabout(){
about_->show();
}
void viewer_mainwin::setData(){
trdlg->setText(ui->textEdit->toPlainText());
ui->action_Print->setEnabled(is_opened);
if (is_opened){
if(ui->textEdit->toPlainText() == ""){
is_empty = true;
qDebug() << "Changed status:textEdit is empty";
}else{
is_empty = false;
qDebug() << "textEdit is not empty";
}
}
}
void viewer_mainwin::trDlgOpen(){
if(trdlg->exec() == QDialog::Rejected){
qWarning() << "Dialog closed unexpectedly: Rejected.";
}
}
void viewer_mainwin::dragEnterEvent(QDragEnterEvent *e){
if(e->mimeData()->hasUrls())
{e->acceptProposedAction();}
}
void viewer_mainwin::dropEvent(QDropEvent *e){
if(e->mimeData()->urls().size() >= 2){
QMessageBox::critical(this,tr("Error"),tr("%1 files have been dragged at the same time. Even multiple files do not support reading.").arg(e->mimeData()->urls().size() ));
}else
{
QString draggedfile = e->mimeData()->urls().first().toLocalFile();
qDebug() << "Load:" << draggedfile;
fileOpen(draggedfile);
}
}