scide: LookupDialog - use more expressive function names
[supercollider.git] / editors / sc-ide / widgets / post_window.cpp
blob678f3719d93a710e9f78fc2f21199bfe2792c4bd
1 /*
2 SuperCollider Qt IDE
3 Copyright (c) 2012 Jakob Leben & Tim Blechmann
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "main_window.hpp"
22 #include "post_window.hpp"
23 #include "../core/main.hpp"
24 #include "../core/settings/manager.hpp"
26 #include <QApplication>
27 #include <QDesktopWidget>
28 #include <QHBoxLayout>
29 #include <QLabel>
30 #include <QPointer>
31 #include <QScrollBar>
32 #include <QShortcut>
33 #include <QToolBar>
35 namespace ScIDE {
37 PostWindow::PostWindow(QWidget* parent):
38 QPlainTextEdit(parent),
39 mNewlinePending(false)
41 setReadOnly(true);
42 setLineWrapMode(QPlainTextEdit::NoWrap);
43 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
45 QRect availableScreenRect = qApp->desktop()->availableGeometry(this);
46 mSizeHint = QSize( availableScreenRect.width() / 2.7, 300 );
48 QAction * action;
50 QAction *copyAction = new QAction(tr("Copy"), this);
51 connect(copyAction, SIGNAL(triggered()), this, SLOT(copy()));
52 copyAction->setShortcut( Main::settings()->shortcut("IDE/shortcuts/copy") );
53 addAction(copyAction);
55 mClearAction = new QAction(tr("Clear"), this);
56 connect(mClearAction, SIGNAL(triggered()), this, SLOT(clear()));
57 addAction(mClearAction);
59 action = new QAction(this);
60 action->setSeparator(true);
61 addAction(action);
63 action = new QAction(tr("Enlarge Post Font"), this);
64 action->setIconText("+");
65 action->setShortcut(tr("Ctrl++", "Enlarge Font"));
66 action->setShortcutContext( Qt::WidgetShortcut );
67 action->setToolTip(tr("Enlarge font"));
68 connect(action, SIGNAL(triggered()), this, SLOT(zoomIn()));
69 addAction(action);
71 action = new QAction(tr("Shrink Post Font"), this);
72 action->setIconText("-");
73 action->setShortcut(tr("Ctrl+-", "Shrink Font"));
74 action->setToolTip(tr("Shrink font"));
75 action->setShortcutContext( Qt::WidgetShortcut );
76 connect(action, SIGNAL(triggered()), this, SLOT(zoomOut()));
77 addAction(action);
79 action = new QAction(this);
80 action->setSeparator(true);
81 addAction(action);
83 mAutoScrollAction = new QAction(tr("Auto Scroll"), this);
84 mAutoScrollAction->setToolTip(tr("Scroll to bottom on new posts"));
85 mAutoScrollAction->setCheckable(true);
86 mAutoScrollAction->setChecked(true);
87 addAction(mAutoScrollAction);
89 setContextMenuPolicy(Qt::ActionsContextMenu);
91 connect(this, SIGNAL(scrollToBottomRequest()),
92 this, SLOT(scrollToBottom()), Qt::QueuedConnection);
93 connect(mAutoScrollAction, SIGNAL(triggered(bool)),
94 this, SLOT(onAutoScrollTriggered(bool)));
96 applySettings( Main::settings() );
99 void PostWindow::applySettings(Settings::Manager * settings)
101 int scrollback = settings->value("IDE/editor/postWindowScrollback").toInt();
102 QFont font = settings->codeFont();
104 setMaximumBlockCount(scrollback);
105 setFont(font);
108 QString PostWindow::symbolUnderCursor()
110 QTextCursor cursor = textCursor();
111 if (!cursor.hasSelection())
112 cursor.select(QTextCursor::WordUnderCursor);
113 return cursor.selectedText();
116 void PostWindow::post(const QString &text)
118 QScrollBar *scrollBar = verticalScrollBar();
119 bool scroll = mAutoScrollAction->isChecked();
121 QTextCursor c(document());
122 c.movePosition(QTextCursor::End);
124 if (mNewlinePending) {
125 c.insertBlock();
126 mNewlinePending = false;
127 c.movePosition(QTextCursor::End);
130 if ( text.endsWith("\n") ) {
131 QString textToInsert (text);
132 textToInsert.chop(1);
133 mNewlinePending = true;
134 c.insertText(textToInsert);
135 } else {
136 c.insertText(text);
139 if (scroll)
140 emit(scrollToBottomRequest());
143 void PostWindow::scrollToBottom()
145 verticalScrollBar()->triggerAction(QAbstractSlider::SliderToMaximum);
148 void PostWindow::onAutoScrollTriggered(bool on)
150 if (on)
151 scrollToBottom();
154 void PostWindow::zoomIn(int steps)
156 zoomFont(steps);
159 void PostWindow::zoomOut(int steps)
161 zoomFont(-steps);
164 void PostWindow::zoomFont(int steps)
166 QFont currentFont = font();
167 const int newSize = currentFont.pointSize() + steps;
168 if (newSize <= 0)
169 return;
170 currentFont.setPointSize(newSize);
171 setFont(currentFont);
174 void PostWindow::wheelEvent( QWheelEvent * e )
176 if (e->modifiers() == Qt::ControlModifier) {
177 if (e->delta() > 0)
178 zoomIn();
179 else
180 zoomOut();
181 return;
184 QPlainTextEdit::wheelEvent(e);
187 bool PostWindow::openDocumentation()
189 return Main::openDocumentation(symbolUnderCursor());
192 void PostWindow::openDefinition()
194 Main::openDefinition(symbolUnderCursor(), MainWindow::instance());
197 void PostWindow::findReferences()
199 Main::findReferences(symbolUnderCursor(), MainWindow::instance());
203 PostDock::PostDock(QWidget* parent):
204 QDockWidget(tr("Post window"), parent)
206 setAllowedAreas(Qt::BottomDockWidgetArea | Qt::RightDockWidgetArea | Qt::LeftDockWidgetArea);
207 setFeatures(DockWidgetFloatable | DockWidgetMovable | DockWidgetClosable);
209 mPostWindow = new PostWindow(this);
210 setWidget(mPostWindow);
212 QToolBar *toolBar = new QToolBar();
213 toolBar->addAction(mPostWindow->mAutoScrollAction);
215 QWidget *titleBar = new QWidget();
216 QHBoxLayout *l = new QHBoxLayout();
217 l->setContentsMargins(5,2,5,0);
218 l->addWidget(new QLabel(windowTitle()), 1);
219 l->addWidget(toolBar);
220 titleBar->setLayout(l);
222 setTitleBarWidget(titleBar);
224 connect(this, SIGNAL(topLevelChanged(bool)), this, SLOT(onFloatingChanged(bool)));
227 void PostDock::onFloatingChanged(bool floating)
229 // HACK: After undocking when main window maximized, the dock widget can not be
230 // resized anymore. Apparently it has to do something with the fact that the dock
231 // widget spans from edge to edge of the screen.
232 // The issue is avoided by slightly shrinking the dock widget.
233 if (floating)
234 resize(size() - QSize(1,1));
237 } // namespace ScIDE