• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdevelop API Reference
  • KDE Home
  • Contact Us
 

kdevelop/kdevplatform/language/codegen

  • extragear
  • kdevelop
  • kdevelop
  • kdevplatform
  • language
  • codegen
applychangeswidget.cpp
Go to the documentation of this file.
1 /* Copyright 2008 Aleix Pol <[email protected]>
2  * Copyright 2009 Ramón Zarazúa <[email protected]>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19 
20 #include "applychangeswidget.h"
21 
22 #include <KTextEditor/Document>
23 
24 #include <KMimeTypeTrader>
25 
26 #include <QAction>
27 #include <QDialogButtonBox>
28 #include <QDir>
29 #include <QLabel>
30 #include <QMimeType>
31 #include <QMimeDatabase>
32 #include <QPushButton>
33 #include <QSplitter>
34 #include <QTemporaryFile>
35 #include <QTabWidget>
36 #include <QVBoxLayout>
37 
38 #include "coderepresentation.h"
39 #include <interfaces/icore.h>
40 #include <interfaces/idocumentcontroller.h>
41 
42 namespace KDevelop {
43 class ApplyChangesWidgetPrivate
44 {
45 public:
46 
47  explicit ApplyChangesWidgetPrivate(ApplyChangesWidget* p)
48  : parent(p)
49  , m_index(0) {}
50  ~ApplyChangesWidgetPrivate()
51  {
52  qDeleteAll(m_temps);
53  }
54 
55  void createEditPart(const KDevelop::IndexedString& url);
56 
57  ApplyChangesWidget* const parent;
58  int m_index;
59  QList<KParts::ReadWritePart*> m_editParts;
60  QList<QTemporaryFile*> m_temps;
61  QList<IndexedString> m_files;
62  QTabWidget* m_documentTabs;
63  QLabel* m_info;
64 };
65 
66 ApplyChangesWidget::ApplyChangesWidget(QWidget* parent)
67  : QDialog(parent)
68  , d_ptr(new ApplyChangesWidgetPrivate(this))
69 {
70  Q_D(ApplyChangesWidget);
71 
72  setSizeGripEnabled(true);
73 
74  auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
75  auto mainLayout = new QVBoxLayout(this);
76  auto okButton = buttonBox->button(QDialogButtonBox::Ok);
77  okButton->setDefault(true);
78  okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
79  connect(buttonBox, &QDialogButtonBox::accepted, this, &ApplyChangesWidget::accept);
80  connect(buttonBox, &QDialogButtonBox::rejected, this, &ApplyChangesWidget::reject);
81 
82  QWidget* w = new QWidget(this);
83  d->m_info = new QLabel(w);
84  d->m_documentTabs = new QTabWidget(w);
85  connect(d->m_documentTabs, &QTabWidget::currentChanged,
86  this, &ApplyChangesWidget::indexChanged);
87 
88  auto* l = new QVBoxLayout(w);
89  l->addWidget(d->m_info);
90  l->addWidget(d->m_documentTabs);
91 
92  mainLayout->addWidget(w);
93  mainLayout->addWidget(buttonBox);
94 
95  resize(QSize(800, 400));
96 }
97 
98 ApplyChangesWidget::~ApplyChangesWidget() = default;
99 
100 bool ApplyChangesWidget::hasDocuments() const
101 {
102  Q_D(const ApplyChangesWidget);
103 
104  return d->m_editParts.size() > 0;
105 }
106 
107 KTextEditor::Document* ApplyChangesWidget::document() const
108 {
109  Q_D(const ApplyChangesWidget);
110 
111  return qobject_cast<KTextEditor::Document*>(d->m_editParts.value(d->m_index));
112 }
113 
114 void ApplyChangesWidget::setInformation(const QString& info)
115 {
116  Q_D(ApplyChangesWidget);
117 
118  d->m_info->setText(info);
119 }
120 
121 void ApplyChangesWidget::addDocuments(const IndexedString& original)
122 {
123  Q_D(ApplyChangesWidget);
124 
125  int idx = d->m_files.indexOf(original);
126  if (idx < 0) {
127  QWidget* w = new QWidget;
128  d->m_documentTabs->addTab(w, original.str());
129  d->m_documentTabs->setCurrentWidget(w);
130 
131  d->m_files.insert(d->m_index, original);
132  d->createEditPart(original);
133  } else {
134  d->m_index = idx;
135  }
136 }
137 
138 bool ApplyChangesWidget::applyAllChanges()
139 {
140  Q_D(ApplyChangesWidget);
141 
143 
144  bool ret = true;
145  for (int i = 0; i < d->m_files.size(); ++i)
146  if (d->m_editParts[i]->saveAs(d->m_files[i].toUrl())) {
147  IDocument* doc = ICore::self()->documentController()->documentForUrl(d->m_files[i].toUrl());
148  if (doc && doc->state() == IDocument::Dirty)
149  doc->reload();
150  } else
151  ret = false;
152 
153  return ret;
154 }
155 }
156 
157 namespace KDevelop {
158 void ApplyChangesWidgetPrivate::createEditPart(const IndexedString& file)
159 {
160  QWidget* widget = m_documentTabs->currentWidget();
161  Q_ASSERT(widget);
162 
163  auto* m = new QVBoxLayout(widget);
164  auto* v = new QSplitter(widget);
165  m->addWidget(v);
166 
167  QUrl url = file.toUrl();
168 
169  QMimeType mimetype = QMimeDatabase().mimeTypeForUrl(url);
170 
171  KParts::ReadWritePart* part = KMimeTypeTrader::self()->createPartInstanceFromQuery<KParts::ReadWritePart>(
172  mimetype.name(), widget, widget);
173  auto* document = qobject_cast<KTextEditor::Document*>(part);
174  Q_ASSERT(document);
175 
176  Q_ASSERT(document->action("file_save"));
177  document->action("file_save")->setEnabled(false);
178 
179  m_editParts.insert(m_index, part);
180 
181  //Open the best code representation, even if it is artificial
182  CodeRepresentation::Ptr repr = createCodeRepresentation(file);
183  if (!repr->fileExists()) {
184  const QString templateName = QDir::tempPath() + QLatin1Char('/') +
185  url.fileName().split(QLatin1Char('.')).last();
186  auto* temp(new QTemporaryFile(templateName));
187  temp->open();
188  temp->write(repr->text().toUtf8());
189  temp->close();
190 
191  url = QUrl::fromLocalFile(temp->fileName());
192 
193  m_temps << temp;
194  }
195  m_editParts[m_index]->openUrl(url);
196 
197  v->addWidget(m_editParts[m_index]->widget());
198  v->setSizes(QList<int> {400, 100});
199 }
200 
201 void ApplyChangesWidget::indexChanged(int newIndex)
202 {
203  Q_D(ApplyChangesWidget);
204 
205  Q_ASSERT(newIndex != -1);
206  d->m_index = newIndex;
207 }
208 
209 void ApplyChangesWidget::updateDiffView(int index)
210 {
211  Q_D(ApplyChangesWidget);
212 
213  d->m_index = index == -1 ? d->m_index : index;
214 }
215 }
QWidget
QDialog::reject
virtual void reject()
KDevelop::ApplyChangesWidget::ApplyChangesWidget
ApplyChangesWidget(QWidget *parent=nullptr)
Definition: applychangeswidget.cpp:66
QDialogButtonBox::rejected
void rejected()
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
KDevelop::ApplyChangesWidget::document
KTextEditor::Document * document() const
Definition: applychangeswidget.cpp:107
QTabWidget
QWidget::resize
void resize(int w, int h)
KDevelop::ApplyChangesWidget
Definition: applychangeswidget.h:34
KDevelop::ApplyChangesWidget::updateDiffView
void updateDiffView(int index=-1)
Update the comparison view fo index, in case changes have been done directly to the opened document...
Definition: applychangeswidget.cpp:209
KDevelop::ApplyChangesWidget::setInformation
void setInformation(const QString &info)
Definition: applychangeswidget.cpp:114
QDir::tempPath
QString tempPath()
applychangeswidget.h
QTabWidget::currentChanged
void currentChanged(int index)
KDevelop::ApplyChangesWidget::hasDocuments
bool hasDocuments() const
Definition: applychangeswidget.cpp:100
KDevelop::ApplyChangesWidget::~ApplyChangesWidget
~ApplyChangesWidget() override
QVBoxLayout
QString
QList< KParts::ReadWritePart * >
KDevelop::CodeRepresentation::Ptr
QExplicitlySharedDataPointer< CodeRepresentation > Ptr
Definition: coderepresentation.h:139
QDialog::accept
virtual void accept()
QSize
QUrl
QLatin1Char
QUrl::fileName
QString fileName() const
QSplitter
QDialogButtonBox
QWidget::QWidget
QWidget(QWidget *parent, QFlags< Qt::WindowType > f)
QList::last
T & last()
KDevelop::ApplyChangesWidget::indexChanged
void indexChanged(int)
Called to signal a change to the currently viewed index.
Definition: applychangeswidget.cpp:201
coderepresentation.h
QDialog
QDialog::setSizeGripEnabled
void setSizeGripEnabled(bool)
QDialogButtonBox::accepted
void accepted()
QTemporaryFile
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QLabel
KDevelop::ApplyChangesWidget::addDocuments
void addDocuments(const IndexedString &original)
Definition: applychangeswidget.cpp:121
KDevelop::createCodeRepresentation
CodeRepresentation::Ptr createCodeRepresentation(const IndexedString &path)
Creates a code-representation for the given url, that allows conveniently accessing its data...
Definition: coderepresentation.cpp:346
QUrl::fromLocalFile
QUrl fromLocalFile(const QString &localFile)
KDevelop::ApplyChangesWidget::applyAllChanges
bool applyAllChanges()
This will save all the modified files into their originals.
Definition: applychangeswidget.cpp:138
This file is part of the KDE documentation.
Documentation copyright © 1996-2019 The KDE developers.
Generated on Thu Dec 12 2019 03:33:20 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kdevelop/kdevplatform/language/codegen

Skip menu "kdevelop/kdevplatform/language/codegen"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdevelop API Reference

Skip menu "kdevelop API Reference"
  •   kdevplatform
  •     debugger
  •     documentation
  •     interfaces
  •     language
  •       assistant
  •       backgroundparser
  •       checks
  •       classmodel
  •       codecompletion
  •       codegen
  •       duchain
  •       editor
  •       highlighting
  •       interfaces
  •       util
  •     outputview
  •     project
  •     serialization
  •     shell
  •     sublime
  •     tests
  •     util
  •     vcs

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal