Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include <cxFilePreviewWidget.h>
34 
35 #include <QTextEdit>
36 #include <QLabel>
37 #include <QTextDocument>
38 #include <QPushButton>
39 #include <QTextStream>
40 //#include <QFileInfo>
41 #include <QFile>
42 #include <iostream>
43 #include "cxTypeConversions.h"
44 #include <QFileSystemWatcher>
45 #include "cxLogger.h"
46 #include "snwSyntaxHighlighter.h"
47 
48 namespace cx
49 {
50 
52  FileWatcherWidget(parent, "FilePreviewWidget", "File Preview"),
53  mTextDocument(new QTextDocument(this)),
54  mTextEdit(new QTextEdit(this)),
55  mSaveButton(new QPushButton("Save", this))
56 {
57  this->setToolTip("Preview and edit a file");
58  mSyntaxHighlighter = NULL;
59  connect(mSaveButton, SIGNAL(clicked()), this, SLOT(saveSlot()));
60  mSaveButton->setEnabled(false);
61 
62  QHBoxLayout* buttonLayout = new QHBoxLayout();
63  buttonLayout->addStretch();
64  buttonLayout->setMargin(0);
65  buttonLayout->addWidget(mSaveButton);
66 
67  QVBoxLayout* layout = new QVBoxLayout(this);
68  layout->setMargin(0);
69  layout->addWidget(mTextEdit);
70  layout->addLayout(buttonLayout);
71 
72  connect(mTextEdit, SIGNAL(textChanged()), this, SLOT(textChangedSlot()));
73  mTextEdit->setDocument(mTextDocument);
74  mTextEdit->setLineWrapMode(QTextEdit::NoWrap);
75 
76  this->setSyntaxHighLighter<snw::SyntaxHighlighter>();
77 }
78 
80 {}
81 
83 {
84  mSaveButton->setEnabled(mTextDocument->isModified());
85 }
86 
87 void FilePreviewWidget::previewFileSlot(const QString& absoluteFilePath)
88 {
89  mSaveButton->setEnabled(false);
90 
91  if(!this->internalOpenNewFile(absoluteFilePath))
92  return;
93 
94  QTextStream stream(mCurrentFile.get());
95  QString text = stream.readAll();
96  mCurrentFile->close();
97 
98  mTextDocument->setPlainText(text);
99  mTextDocument->setModified(false);
100 
101  this->textChangedSlot();
102 }
103 
105 {
106  if(!mCurrentFile)
107  return;
108 
109  if(!mCurrentFile->open(QIODevice::WriteOnly | QIODevice::Truncate))
110  {
111  reportWarning("Could not open file "+mCurrentFile->fileName());
112  return;
113  }
114 
115  mCurrentFile->write(mTextDocument->toPlainText().toLatin1());
116  mCurrentFile->close();
117 
118  mTextDocument->setModified(false);
119 
120 }
121 
122 }//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:91
Baseclass for widgets that should watch a file.
FilePreviewWidget(QWidget *parent)