Fraxinus  18.10
An IGT application
cxFilePreviewWidget.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include <cxFilePreviewWidget.h>
13 
14 #include <QTextEdit>
15 #include <QLabel>
16 #include <QTextDocument>
17 #include <QPushButton>
18 #include <QTextStream>
19 //#include <QFileInfo>
20 #include <QFile>
21 #include <iostream>
22 #include "cxTypeConversions.h"
23 #include <QFileSystemWatcher>
24 #include "cxLogger.h"
25 #include "snwSyntaxHighlighter.h"
26 
27 namespace cx
28 {
29 
31  FileWatcherWidget(parent, "file_preview_widget", "File Preview"),
32  mTextDocument(new QTextDocument(this)),
33  mTextEdit(new QTextEdit(this)),
34  mSaveButton(new QPushButton("Save", this))
35 {
36  this->setToolTip("Preview and edit a file");
37  mSyntaxHighlighter = NULL;
38  connect(mSaveButton, SIGNAL(clicked()), this, SLOT(saveSlot()));
39  mSaveButton->setEnabled(false);
40 
41  QHBoxLayout* buttonLayout = new QHBoxLayout();
42  buttonLayout->addStretch();
43  buttonLayout->setMargin(0);
44  buttonLayout->addWidget(mSaveButton);
45 
46  QVBoxLayout* layout = new QVBoxLayout(this);
47  layout->setMargin(0);
48  layout->addWidget(mTextEdit);
49  layout->addLayout(buttonLayout);
50 
51  connect(mTextEdit, SIGNAL(textChanged()), this, SLOT(textChangedSlot()));
52  mTextEdit->setDocument(mTextDocument);
53  mTextEdit->setLineWrapMode(QTextEdit::NoWrap);
54 
55  this->setSyntaxHighLighter<snw::SyntaxHighlighter>();
56 }
57 
59 {}
60 
62 {
63  mSaveButton->setEnabled(mTextDocument->isModified());
64 }
65 
66 void FilePreviewWidget::previewFileSlot(const QString& absoluteFilePath)
67 {
68  mSaveButton->setEnabled(false);
69 
70  if(!this->internalOpenNewFile(absoluteFilePath))
71  return;
72 
73  QTextStream stream(mCurrentFile.get());
74  QString text = stream.readAll();
75  mCurrentFile->close();
76 
77  mTextDocument->setPlainText(text);
78  mTextDocument->setModified(false);
79 
80  this->textChangedSlot();
81 }
82 
84 {
85  if(!mCurrentFile)
86  return;
87 
88  if(!mCurrentFile->open(QIODevice::WriteOnly | QIODevice::Truncate))
89  {
90  reportWarning("Could not open file "+mCurrentFile->fileName());
91  return;
92  }
93 
94  mCurrentFile->write(mTextDocument->toPlainText().toLatin1());
95  mCurrentFile->close();
96 
97  mTextDocument->setModified(false);
98 
99 }
100 
101 }//namespace cx
bool internalOpenNewFile(const QString absoluteFilePath)
boost::shared_ptr< QFile > mCurrentFile
virtual void previewFileSlot(const QString &absoluteFilePath)
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
Baseclass for widgets that should watch a file.
FilePreviewWidget(QWidget *parent)
Namespace for all CustusX production code.