1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25
26 #include "fileutils.h"
27 #include "reloadpromptutils.h"
28
29 #include <QCoreApplication>
30 #include <QDir>
31 #include <QMessageBox>
32 #include <QPushButton>
33
34 namespace Utils {
35
reloadPrompt(const FilePath & fileName,bool modified,bool enableDiffOption,QWidget * parent)36 QTCREATOR_UTILS_EXPORT ReloadPromptAnswer reloadPrompt(const FilePath &fileName,
37 bool modified,
38 bool enableDiffOption,
39 QWidget *parent)
40 {
41
42 const QString title = QCoreApplication::translate("Utils::reloadPrompt", "File Changed");
43 QString msg;
44
45 if (modified) {
46 msg = QCoreApplication::translate("Utils::reloadPrompt",
47 "The unsaved file <i>%1</i> has been changed on disk. "
48 "Do you want to reload it and discard your changes?");
49 } else {
50 msg = QCoreApplication::translate("Utils::reloadPrompt",
51 "The file <i>%1</i> has been changed on disk. Do you want to reload it?");
52 }
53 msg = "<p>" + msg.arg(fileName.fileName()) + "</p><p>" +
54 QCoreApplication::translate("Utils::reloadPrompt",
55 "The default behavior can be set in Tools > Options > Environment > System.")
56 + "</p>";
57 return reloadPrompt(title, msg, fileName.toUserOutput(), enableDiffOption, parent);
58 }
59
reloadPrompt(const QString & title,const QString & prompt,const QString & details,bool enableDiffOption,QWidget * parent)60 QTCREATOR_UTILS_EXPORT ReloadPromptAnswer reloadPrompt(const QString &title,
61 const QString &prompt,
62 const QString &details,
63 bool enableDiffOption,
64 QWidget *parent)
65 {
66 QMessageBox msg(parent);
67 msg.setStandardButtons(QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::Close
68 | QMessageBox::No | QMessageBox::NoToAll);
69 msg.setDefaultButton(QMessageBox::YesToAll);
70 msg.setWindowTitle(title);
71 msg.setText(prompt);
72 msg.setDetailedText(details);
73
74 msg.button(QMessageBox::Close)->setText(QCoreApplication::translate("Utils::reloadPrompt",
75 "&Close"));
76
77 QPushButton *diffButton = nullptr;
78 if (enableDiffOption) {
79 diffButton = msg.addButton(QCoreApplication::translate(
80 "Utils::reloadPrompt", "No to All && &Diff"),
81 QMessageBox::NoRole);
82 }
83
84 const int result = msg.exec();
85
86 if (msg.clickedButton() == diffButton)
87 return ReloadNoneAndDiff;
88
89 switch (result) {
90 case QMessageBox::Yes:
91 return ReloadCurrent;
92 case QMessageBox::YesToAll:
93 return ReloadAll;
94 case QMessageBox::No:
95 return ReloadSkipCurrent;
96 case QMessageBox::Close:
97 return CloseCurrent;
98 default:
99 break;
100 }
101 return ReloadNone;
102 }
103
104 QTCREATOR_UTILS_EXPORT FileDeletedPromptAnswer
fileDeletedPrompt(const QString & fileName,QWidget * parent)105 fileDeletedPrompt(const QString &fileName, QWidget *parent)
106 {
107 const QString title = QCoreApplication::translate("Utils::fileDeletedPrompt",
108 "File Has Been Removed");
109 QString msg = QCoreApplication::translate("Utils::fileDeletedPrompt",
110 "The file %1 has been removed from disk. "
111 "Do you want to save it under a different name, or close "
112 "the editor?").arg(QDir::toNativeSeparators(fileName));
113 QMessageBox box(QMessageBox::Question, title, msg, QMessageBox::NoButton, parent);
114 QPushButton *close =
115 box.addButton(QCoreApplication::translate("Utils::fileDeletedPrompt", "&Close"),
116 QMessageBox::RejectRole);
117 QPushButton *closeAll =
118 box.addButton(QCoreApplication::translate("Utils::fileDeletedPrompt", "C&lose All"),
119 QMessageBox::RejectRole);
120 QPushButton *saveas =
121 box.addButton(QCoreApplication::translate("Utils::fileDeletedPrompt", "Save &as..."),
122 QMessageBox::ActionRole);
123 QPushButton *save =
124 box.addButton(QCoreApplication::translate("Utils::fileDeletedPrompt", "&Save"),
125 QMessageBox::AcceptRole);
126 box.setDefaultButton(saveas);
127 box.exec();
128 QAbstractButton *clickedbutton = box.clickedButton();
129 if (clickedbutton == close)
130 return FileDeletedClose;
131 else if (clickedbutton == closeAll)
132 return FileDeletedCloseAll;
133 else if (clickedbutton == saveas)
134 return FileDeletedSaveAs;
135 else if (clickedbutton == save)
136 return FileDeletedSave;
137 return FileDeletedClose;
138 }
139
140 } // namespace Utils
141