CustusX  2023.01.05-dev+develop.0da12
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 #include "cxFilePreviewProperty.h"
27 
28 namespace cx
29 {
30 
31 FilePreviewWidget::FilePreviewWidget(QWidget* parent, FilePreviewPropertyPtr dataInterface, QGridLayout *gridLayout, int row) :
32  FileWatcherWidget(parent, "file_preview_widget", "File Preview"),
33  mTextDocument(new QTextDocument(this)),
34  mTextEdit(new QTextEdit(this)),
35  mSaveButton(new QPushButton("Save", this))
36 {
37  mData = dataInterface;
38  if(mData) //Allow widget to be used like before without a dataInterface
39  {
40  //connect(mData.get(), SIGNAL(changed()), this, SLOT(setModified()));
41  connect(mData.get(), &Property::changed, this, &FilePreviewWidget::fileinterfaceChanged);
42  }
43 
44  this->setToolTip("Preview and edit a file");
45  mSyntaxHighlighter = NULL;
46  connect(mSaveButton, SIGNAL(clicked()), this, SLOT(saveSlot()));
47  mSaveButton->setEnabled(false);
48 
49  connect(mTextEdit, SIGNAL(textChanged()), this, SLOT(textChangedSlot()));
50  mTextEdit->setDocument(mTextDocument);
51  mTextEdit->setLineWrapMode(QTextEdit::NoWrap);
52 
53  this->setSyntaxHighLighter<snw::SyntaxHighlighter>();
54 
55  this->createAndAddEditorLayout(gridLayout, row);
56 
57  //this->setModified();//Needed?
58  this->fileinterfaceChanged();
59 }
60 
61 void FilePreviewWidget::createAndAddEditorLayout(QGridLayout *gridLayout, int row)
62 {
63  QHBoxLayout* buttonLayout = new QHBoxLayout();
64  buttonLayout->addStretch();
65  buttonLayout->setMargin(0);
66  buttonLayout->addWidget(mSaveButton);
67 
68  QVBoxLayout* layout;
69  if (gridLayout)
70  layout = new QVBoxLayout();
71  else
72  layout = new QVBoxLayout(this);
73 
74  layout->setMargin(0);
75  layout->addWidget(mTextEdit);
76  layout->addLayout(buttonLayout);
77 
78  if (gridLayout) // add to input gridlayout
79  gridLayout->addLayout(layout, row, 0);
80 }
81 
83 {}
84 
86 {
87  mSaveButton->setEnabled(mTextDocument->isModified());
88 }
89 
91 {
92  if(mData)
93  this->previewFileSlot(mData->getEmbeddedPath().getAbsoluteFilepath());
94 }
95 void FilePreviewWidget::previewFileSlot(const QString& absoluteFilePath)
96 {
97  mSaveButton->setEnabled(false);
98 
99  if(!this->internalOpenNewFile(absoluteFilePath))
100  return;
101 
102  QTextStream stream(mCurrentFile.get());
103  QString text = stream.readAll();
104  mCurrentFile->close();
105 
106  mTextDocument->setPlainText(text);
107  mTextDocument->setModified(false);
108 
109  this->textChangedSlot();
110 }
111 
113 {
114  if(!mCurrentFile)
115  return;
116 
117  if(!mCurrentFile->open(QIODevice::WriteOnly | QIODevice::Truncate))
118  {
119  reportWarning("Could not open file "+mCurrentFile->fileName());
120  return;
121  }
122 
123  mCurrentFile->write(mTextDocument->toPlainText().toLatin1());
124  mCurrentFile->close();
125 
126  mTextDocument->setModified(false);
127 
128 }
129 
130 }//namespace cx
bool internalOpenNewFile(const QString absoluteFilePath)
boost::shared_ptr< class FilePreviewProperty > FilePreviewPropertyPtr
boost::shared_ptr< QFile > mCurrentFile
virtual void previewFileSlot(const QString &absoluteFilePath)
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
void changed()
emit when the underlying data value is changed: The user interface will be updated.
Baseclass for widgets that should watch a file.
FilePreviewWidget(QWidget *parent, FilePreviewPropertyPtr dataInterface=FilePreviewPropertyPtr(), QGridLayout *gridLayout=0, int row=0)
Namespace for all CustusX production code.