Changeset View
Changeset View
Standalone View
Standalone View
language/duchain/navigation/problemnavigationcontext.cpp
Show All 12 Lines | 1 | /* | |||
---|---|---|---|---|---|
13 | You should have received a copy of the GNU Library General Public License | 13 | You should have received a copy of the GNU Library General Public License | ||
14 | along with this library; see the file COPYING.LIB. If not, write to | 14 | along with this library; see the file COPYING.LIB. If not, write to | ||
15 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 15 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
16 | Boston, MA 02110-1301, USA. | 16 | Boston, MA 02110-1301, USA. | ||
17 | */ | 17 | */ | ||
18 | 18 | | |||
19 | #include "problemnavigationcontext.h" | 19 | #include "problemnavigationcontext.h" | ||
20 | 20 | | |||
21 | #include "util/debug.h" | ||||
22 | | ||||
21 | #include <QHBoxLayout> | 23 | #include <QHBoxLayout> | ||
22 | #include <QMenu> | 24 | #include <QMenu> | ||
23 | 25 | | |||
24 | #include <KLocalizedString> | 26 | #include <KLocalizedString> | ||
25 | 27 | | |||
26 | #include <language/duchain/declaration.h> | 28 | #include <language/duchain/declaration.h> | ||
27 | #include <language/duchain/duchainlock.h> | 29 | #include <language/duchain/duchainlock.h> | ||
28 | #include <language/duchain/duchainutils.h> | 30 | #include <language/duchain/duchainutils.h> | ||
29 | #include <language/duchain/problem.h> | 31 | #include <language/duchain/problem.h> | ||
30 | #include <interfaces/iassistant.h> | | |||
31 | #include <util/richtextpushbutton.h> | 32 | #include <util/richtextpushbutton.h> | ||
32 | 33 | | |||
33 | using namespace KDevelop; | 34 | using namespace KDevelop; | ||
34 | 35 | | |||
36 | namespace { | ||||
37 | | ||||
38 | QString KEY_START_ASSISTANT() { return QStringLiteral("start_assistant"); } | ||||
39 | QString KEY_INVOKE_ACTION(int num) { return QStringLiteral("invoke_action_%1").arg(num); } | ||||
40 | | ||||
41 | } | ||||
42 | | ||||
43 | | ||||
44 | KDevelop::AssistantNavigationContext::AssistantNavigationContext(const IAssistant::Ptr& assistant) | ||||
45 | : m_assistant(assistant) | ||||
46 | { | ||||
47 | } | ||||
48 | | ||||
49 | KDevelop::AssistantNavigationContext::~AssistantNavigationContext() | ||||
50 | { | ||||
51 | } | ||||
52 | | ||||
53 | | ||||
54 | QString KDevelop::AssistantNavigationContext::name() const | ||||
55 | { | ||||
56 | return i18n("Assistant"); | ||||
57 | } | ||||
58 | | ||||
59 | QString KDevelop::AssistantNavigationContext::html(bool shorten) | ||||
60 | { | ||||
61 | clear(); | ||||
62 | m_shorten = shorten; | ||||
63 | | ||||
64 | modifyHtml() += i18n("Solutions:") + "<br/>"; | ||||
65 | | ||||
66 | int index = 0; | ||||
67 | foreach (auto assistantAction, m_assistant->actions()) { | ||||
68 | makeLink(i18n("Apply solution %1", index), KEY_INVOKE_ACTION(index), | ||||
69 | NavigationAction(KEY_INVOKE_ACTION(index))); | ||||
70 | modifyHtml() += ": " + assistantAction->description().toHtmlEscaped() + "<br/>"; | ||||
71 | ++index; | ||||
72 | } | ||||
73 | | ||||
74 | return currentHtml(); | ||||
75 | } | ||||
76 | | ||||
77 | NavigationContextPointer AssistantNavigationContext::executeKeyAction(QString key) | ||||
mwolff: const& | |||||
78 | { | ||||
79 | if (key.startsWith(QLatin1String("invoke_action_"))) { | ||||
80 | if (!m_assistant) | ||||
81 | return {}; | ||||
82 | | ||||
83 | const auto index = key.replace(QLatin1String("invoke_action_"), QString()).toInt(); | ||||
84 | auto action = m_assistant->actions().value(index); | ||||
85 | if (action) { | ||||
86 | action->execute(); | ||||
87 | } else { | ||||
88 | qCWarning(LANGUAGE()) << "Action got removed in-between"; | ||||
89 | return {}; | ||||
90 | } | ||||
91 | } | ||||
92 | | ||||
93 | return {}; | ||||
94 | } | ||||
95 | | ||||
96 | | ||||
35 | ProblemNavigationContext::ProblemNavigationContext(const IProblem::Ptr& problem) | 97 | ProblemNavigationContext::ProblemNavigationContext(const IProblem::Ptr& problem) | ||
36 | : m_problem(problem) | 98 | : m_problem(problem) | ||
37 | , m_widget(nullptr) | 99 | , m_widget(nullptr) | ||
38 | { | 100 | { | ||
39 | QExplicitlySharedDataPointer< IAssistant > solution = problem->solutionAssistant(); | | |||
40 | if(solution && !solution->actions().isEmpty()) { | | |||
41 | m_widget = new QWidget; | | |||
42 | QHBoxLayout* layout = new QHBoxLayout(m_widget); | | |||
43 | RichTextPushButton* button = new RichTextPushButton; | | |||
44 | // button->setPopupMode(QToolButton::InstantPopup); | | |||
45 | if(!solution->title().isEmpty()) | | |||
46 | button->setHtml(i18n("Solve: %1", solution->title())); | | |||
47 | else | | |||
48 | button->setHtml(i18n("Solve")); | | |||
49 | | ||||
50 | QMenu* menu = new QMenu; | | |||
51 | menu->setFocusPolicy(Qt::NoFocus); | | |||
52 | foreach(IAssistantAction::Ptr action, solution->actions()) { | | |||
53 | menu->addAction(action->toKAction(this)); | | |||
54 | } | | |||
55 | button->setMenu(menu); | | |||
56 | | ||||
57 | layout->addWidget(button); | | |||
58 | layout->setAlignment(button, Qt::AlignLeft); | | |||
59 | m_widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum); | | |||
60 | } | | |||
61 | } | 101 | } | ||
62 | 102 | | |||
63 | ProblemNavigationContext::~ProblemNavigationContext() | 103 | ProblemNavigationContext::~ProblemNavigationContext() | ||
64 | { | 104 | { | ||
65 | delete m_widget; | 105 | delete m_widget; | ||
66 | } | 106 | } | ||
67 | 107 | | |||
68 | QWidget* ProblemNavigationContext::widget() const | 108 | QWidget* ProblemNavigationContext::widget() const | ||
69 | { | 109 | { | ||
70 | return m_widget; | 110 | return m_widget; | ||
71 | } | 111 | } | ||
72 | 112 | | |||
73 | bool ProblemNavigationContext::isWidgetMaximized() const | 113 | bool ProblemNavigationContext::isWidgetMaximized() const | ||
74 | { | 114 | { | ||
75 | return false; | 115 | return false; | ||
76 | } | 116 | } | ||
77 | 117 | | |||
78 | QString ProblemNavigationContext::name() const | 118 | QString ProblemNavigationContext::name() const | ||
79 | { | 119 | { | ||
80 | return i18n("Problem"); | 120 | return i18n("Problem"); | ||
81 | } | 121 | } | ||
82 | 122 | | |||
83 | | ||||
84 | QString ProblemNavigationContext::html(bool shorten) | 123 | QString ProblemNavigationContext::html(bool shorten) | ||
85 | { | 124 | { | ||
86 | clear(); | 125 | clear(); | ||
87 | m_shorten = shorten; | 126 | m_shorten = shorten; | ||
88 | modifyHtml() += QStringLiteral("<html><body><p>"); | 127 | modifyHtml() += QStringLiteral("<html><body><p>"); | ||
89 | 128 | | |||
90 | modifyHtml() += i18n("Problem in <b>%1</b>:<br/>", m_problem->sourceString()); | 129 | modifyHtml() += i18n("Problem in %1: ", m_problem->sourceString()); | ||
130 | auto assistant = m_problem->solutionAssistant(); | ||||
131 | if (assistant && !assistant->actions().isEmpty()) { | ||||
132 | makeLink(i18n("Start Assistant (%1 solutions)", assistant->actions().count()), KEY_START_ASSISTANT(), | ||||
133 | NavigationAction(KEY_START_ASSISTANT())); | ||||
134 | modifyHtml() += QStringLiteral("<br/>"); | ||||
135 | } | ||||
136 | | ||||
91 | modifyHtml() += m_problem->description().toHtmlEscaped(); | 137 | modifyHtml() += m_problem->description().toHtmlEscaped(); | ||
92 | modifyHtml() += QStringLiteral("<br/>"); | 138 | modifyHtml() += QStringLiteral("<br/>"); | ||
93 | modifyHtml() += "<i style=\"white-space:pre-wrap\">" + m_problem->explanation().toHtmlEscaped() + "</i>"; | 139 | modifyHtml() += "<i style=\"white-space:pre-wrap\">" + m_problem->explanation().toHtmlEscaped() + "</i>"; | ||
94 | 140 | | |||
95 | const QVector<IProblem::Ptr> diagnostics = m_problem->diagnostics(); | 141 | const QVector<IProblem::Ptr> diagnostics = m_problem->diagnostics(); | ||
96 | if (!diagnostics.isEmpty()) { | 142 | if (!diagnostics.isEmpty()) { | ||
97 | modifyHtml() += QStringLiteral("<br/>"); | 143 | modifyHtml() += QStringLiteral("<br/>"); | ||
98 | 144 | | |||
Show All 22 Lines | 146 | for (auto diagnostic : diagnostics) { | |||
121 | } | 167 | } | ||
122 | modifyHtml() += QStringLiteral("<br/>"); | 168 | modifyHtml() += QStringLiteral("<br/>"); | ||
123 | } | 169 | } | ||
124 | } | 170 | } | ||
125 | 171 | | |||
126 | modifyHtml() += QStringLiteral("</p></body></html>"); | 172 | modifyHtml() += QStringLiteral("</p></body></html>"); | ||
127 | return currentHtml(); | 173 | return currentHtml(); | ||
128 | } | 174 | } | ||
175 | | ||||
176 | NavigationContextPointer ProblemNavigationContext::executeKeyAction(QString key) | ||||
mwolff: const& | |||||
177 | { | ||||
178 | if (key == KEY_START_ASSISTANT()) { | ||||
179 | auto assistant = m_problem->solutionAssistant(); | ||||
180 | if (!assistant) | ||||
181 | return {}; | ||||
182 | | ||||
183 | return NavigationContextPointer(new AssistantNavigationContext(assistant)); | ||||
184 | } | ||||
185 | | ||||
186 | return {}; | ||||
187 | } |
const&