| 1 | /* |
| 2 | Copyright (C) 2008, 2009 Andres Cabrera |
| 3 | [email protected] |
| 4 | |
| 5 | This file is part of CsoundQt. |
| 6 | |
| 7 | CsoundQt is free software; you can redistribute it |
| 8 | and/or modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation; either |
| 10 | version 2.1 of the License, or (at your option) any later version. |
| 11 | |
| 12 | CsoundQt is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with Csound; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 20 | 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include "dockhelp.h" |
| 24 | #include "ui_dockhelp.h" |
| 25 | |
| 26 | #include <QtWidgets> |
| 27 | |
| 28 | |
| 29 | DockHelp::DockHelp(QWidget *parent) |
| 30 | : QDockWidget(parent), ui(new Ui::DockHelp) |
| 31 | { |
| 32 | ui->setupUi(this); |
| 33 | findFlags = 0; |
| 34 | setWindowTitle("Help"); // titlebar and overall layout |
| 35 | setMinimumSize(400,200); |
| 36 | |
| 37 | ui->backButton->setIcon(QIcon(":/themes/breeze/br_prev.png")); |
| 38 | ui->forwardButton->setIcon(QIcon(":/themes/breeze/br_next.png")); |
| 39 | |
| 40 | connect(ui->toggleFindButton, SIGNAL(toggled(bool)), this, SLOT(toggleFindBarVisible(bool))); |
| 41 | connect(ui->backButton, SIGNAL(released()), this, SLOT(browseBack())); |
| 42 | connect(ui->forwardButton, SIGNAL(released()), this, SLOT(browseForward())); |
| 43 | // connect(ui->opcodesToolButton, SIGNAL(released()), this, SLOT(showOverview())); |
| 44 | connect(ui->homeToolButton, SIGNAL(released()), this, SLOT(showManual())); |
| 45 | connect(ui->findLine,SIGNAL(returnPressed()),this,SLOT(onReturnPressed())); |
| 46 | connect(ui->findLine,SIGNAL(textEdited(QString)),this,SLOT(onTextChanged())); |
| 47 | ui->findPreviousAct->setShortcut(QKeySequence::FindPrevious); |
| 48 | ui->nextFindAct->setShortcut(QKeySequence::FindNext); |
| 49 | connect(ui->findPreviousAct,SIGNAL(triggered()),this,SLOT(onPreviousButtonPressed())); |
| 50 | connect(ui->nextFindAct,SIGNAL(triggered()),this,SLOT(onNextButtonPressed())); |
| 51 | ui->previousFindButton->setDefaultAction(ui->findPreviousAct); |
| 52 | ui->nextFindButton->setDefaultAction(ui->nextFindAct); |
| 53 | |
| 54 | connect(ui->caseBox,SIGNAL(stateChanged(int)),this,SLOT(onCaseBoxChanged(int))); |
| 55 | connect(ui->wholeWordBox,SIGNAL(stateChanged(int)),this,SLOT(onWholeWordBoxChanged(int))); |
| 56 | connect(ui->text, SIGNAL(anchorClicked(QUrl)), this, SLOT(followLink(QUrl))); |
| 57 | |
| 58 | ui->toggleFindButton->setChecked(false); |
| 59 | ui->findLine->setVisible(false); |
| 60 | ui->caseBox->setVisible(false); |
| 61 | ui->wholeWordBox->setVisible(false); |
| 62 | ui->label->setVisible(false); |
| 63 | ui->nextFindButton->setVisible(false); |
| 64 | ui->previousFindButton->setVisible(false); |
| 65 | |
| 66 | |
| 67 | styleSheetLight = ( |
| 68 | "body { " |
| 69 | " background-color: #f8f8f8 !important; " |
| 70 | " color: #101010; " |
| 71 | "} " |
| 72 | "a { text-decoration: none; } " |
| 73 | "td.linenos { background-color: #f0f0f0; padding-right: 10px; } " |
| 74 | "span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; } " |
| 75 | "pre { line-height: 100%; " |
| 76 | " background-color: #f2f2f2 !important; " |
| 77 | " font-size: 92% !important;} " |
| 78 | "code { " |
| 79 | " color: #202020 !important; " |
| 80 | " font-size: 92% !important; " |
| 81 | " border: none !important; " |
| 82 | "} " |
| 83 | "body .hll { background-color: #ffffcc } " |
| 84 | "body .c { color: #408080; font-style: italic } /* Comment */ " |
| 85 | "body .err { border: 1px solid #FF0000 } /* Error */ " |
| 86 | "body .k { color: #008000; font-weight: bold } /* Keyword */ " |
| 87 | "body .o { color: #666666 } /* Operator */ " |
| 88 | "body .ch { color: #408080; font-style: italic } /* Comment.Hashbang */ " |
| 89 | "body .cm { color: #408080; font-style: italic } /* Comment.Multiline */ " |
| 90 | "body .cp { color: #BC7A00 } /* Comment.Preproc */ " |
| 91 | "body .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */ " |
| 92 | "body .c1 { color: #408080; font-style: italic } /* Comment.Single */ " |
| 93 | "body .cs { color: #408080; font-style: italic } /* Comment.Special */ " |
| 94 | "body .gd { color: #A00000 } /* Generic.Deleted */ " |
| 95 | "body .ge { font-style: italic } /* Generic.Emph */ " |
| 96 | "body .gr { color: #FF0000 } /* Generic.Error */ " |
| 97 | "body .gh { color: #000080; font-weight: bold } /* Generic.Heading */ " |
| 98 | "body .gi { color: #00A000 } /* Generic.Inserted */ " |
| 99 | "body .go { color: #888888 } /* Generic.Output */ " |
| 100 | "body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */ " |
| 101 | "body .gs { font-weight: bold } /* Generic.Strong */ " |
| 102 | "body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */ " |
| 103 | "body .gt { color: #0044DD } /* Generic.Traceback */ " |
| 104 | "body .kc { color: #008000; font-weight: bold } /* Keyword.Constant */ " |
| 105 | "body .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */ " |
| 106 | "body .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */ " |
| 107 | "body .kp { color: #008000 } /* Keyword.Pseudo */ " |
| 108 | "body .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */ " |
| 109 | "body .kt { color: #B00040 } /* Keyword.Type */ " |
| 110 | "body .m { color: #666666 } /* Literal.Number */ " |
| 111 | "body .s { color: #BA2121 } /* Literal.String */ " |
| 112 | "body .na { color: #7D9029 } /* Name.Attribute */ " |
| 113 | "body .nb { color: #008000 } /* Name.Builtin */ " |
| 114 | "body .nc { color: #0000FF; font-weight: bold } /* Name.Class */ " |
| 115 | "body .no { color: #880000 } /* Name.Constant */ " |
| 116 | "body .nd { color: #AA22FF } /* Name.Decorator */ " |
| 117 | "body .ni { color: #999999; font-weight: bold } /* Name.Entity */ " |
| 118 | "body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */ " |
| 119 | "body .nf { color: #0000FF } /* Name.Function */ " |
| 120 | "body .nl { color: #A0A000 } /* Name.Label */ " |
| 121 | "body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */ " |
| 122 | "body .nt { color: #008000; font-weight: bold } /* Name.Tag */ " |
| 123 | "body .nv { color: #19177C } /* Name.Variable */ " |
| 124 | "body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */ " |
| 125 | "body .w { color: #bbbbbb } /* Text.Whitespace */ " |
| 126 | "body .mb { color: #666666 } /* Literal.Number.Bin */ " |
| 127 | "body .mf { color: #666666 } /* Literal.Number.Float */ " |
| 128 | "body .mh { color: #666666 } /* Literal.Number.Hex */ " |
| 129 | "body .mi { color: #666666 } /* Literal.Number.Integer */ " |
| 130 | "body .mo { color: #666666 } /* Literal.Number.Oct */ " |
| 131 | "body .sa { color: #BA2121 } /* Literal.String.Affix */ " |
| 132 | "body .sb { color: #BA2121 } /* Literal.String.Backtick */ " |
| 133 | "body .sc { color: #BA2121 } /* Literal.String.Char */ " |
| 134 | "body .dl { color: #BA2121 } /* Literal.String.Delimiter */ " |
| 135 | "body .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */ " |
| 136 | "body .s2 { color: #BA2121 } /* Literal.String.Double */ " |
| 137 | "body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */ " |
| 138 | "body .sh { color: #BA2121 } /* Literal.String.Heredoc */ " |
| 139 | "body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */" |
| 140 | "body .sx { color: #008000 } /* Literal.String.Other */ " |
| 141 | "body .sr { color: #BB6688 } /* Literal.String.Regex */ " |
| 142 | "body .s1 { color: #BA2121 } /* Literal.String.Single */ " |
| 143 | "body .ss { color: #19177C } /* Literal.String.Symbol */ " |
| 144 | "body .bp { color: #008000 } /* Name.Builtin.Pseudo */ " |
| 145 | "body .fm { color: #0000FF } /* Name.Function.Magic */ " |
| 146 | "body .vc { color: #19177C } /* Name.Variable.Class */ " |
| 147 | "body .vg { color: #19177C } /* Name.Variable.Global */ " |
| 148 | "body .vi { color: #19177C } /* Name.Variable.Instance */ " |
| 149 | "body .vm { color: #19177C } /* Name.Variable.Magic */ " |
| 150 | "body .il { color: #666666 } /* Literal.Number.Integer.Long */ " |
| 151 | ); |
| 152 | ui->text->document()->setDefaultStyleSheet(styleSheetLight); |
| 153 | ui->text->setStyleSheet( |
| 154 | "QTextEdit { " |
| 155 | " background-color: #f0f0f0; " |
| 156 | " color: #202020; " |
| 157 | "} " |
| 158 | |
| 159 | ); |
| 160 | } |
| 161 | |
| 162 | DockHelp::~DockHelp() |
| 163 | { |
| 164 | } |
| 165 | |
| 166 | bool DockHelp::hasFocus() |
| 167 | { |
| 168 | return QDockWidget::hasFocus() |
| 169 | || ui->text->hasFocus() |
| 170 | || ui->findLine->hasFocus(); |
| 171 | } |
| 172 | |
| 173 | void DockHelp::loadFile(QString fileName, QString anchor) { |
| 174 | if(!QFile::exists(fileName)) { |
| 175 | ui->text->setText(tr("Not Found! Make sure the documentation path is set in the Configuration Dialog.")); |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | //#ifdef Q_OS_WIN32 |
| 180 | // QStringList searchPaths; |
| 181 | // searchPaths << docDir; |
| 182 | // ui->text->setSearchPaths(searchPaths); |
| 183 | // QTextStream in(&file); |
| 184 | // in.setAutoDetectUnicode(true); |
| 185 | // ui->text->setHtml(in.readAll()); |
| 186 | //#else |
| 187 | |
| 188 | // seems that setSource work now for Windows too. Needs testing |
| 189 | if (QFileInfo(fileName).suffix() == ".md") { |
| 190 | QFile file(fileName); |
| 191 | QTextStream md(&file); |
| 192 | #if QT_VERSION >= 0x051400 |
| 193 | ui->text->document()->setMarkdown(md.readAll()); |
| 194 | ui->text->setHtml(ui->text->document()->toHtml()); |
| 195 | #else |
| 196 | ui->text->document()->setPlainText(md.readAll()); |
| 197 | #endif |
| 198 | } |
| 199 | else { |
| 200 | QUrl url = QUrl::fromLocalFile(fileName); |
| 201 | if(!anchor.isEmpty()) { |
| 202 | url.setUrl(url.toString() + "#" + anchor); |
| 203 | } |
| 204 | qDebug() << "url:" << url << url.toString(); |
| 205 | ui->text->setSource(url); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | //#endif |
| 210 | |
| 211 | } |
| 212 | |
| 213 | void DockHelp::setIconTheme(QString theme) |
| 214 | { |
| 215 | ui->backButton->setIcon(QIcon(QString(":/themes/%1/browse-prev.png").arg(theme))); |
| 216 | ui->forwardButton->setIcon(QIcon(QString(":/themes/%1/browse-next.png").arg(theme))); |
| 217 | ui->homeToolButton->setIcon(QIcon(QString(":/themes/%1/home.png").arg(theme))); |
| 218 | ui->toggleFindButton->setIcon(QIcon(QString(":/themes/%1/edit-find.png").arg(theme))); |
| 219 | ui->previousFindButton->setIcon(QIcon(QString(":/themes/%1/browse-prev.png").arg(theme))); |
| 220 | ui->nextFindButton->setIcon(QIcon(QString(":/themes/%1/browse-next.png").arg(theme))); |
| 221 | } |
| 222 | |
| 223 | void DockHelp::changeFontSize(int change) |
| 224 | { |
| 225 | QFont font = ui->text->font(); |
| 226 | int currentSize = font.pointSize(); |
| 227 | font.setPointSize(currentSize+change); |
| 228 | ui->text->setFont(font); |
| 229 | } |
| 230 | |
| 231 | void DockHelp::closeEvent(QCloseEvent * /*event*/) |
| 232 | { |
| 233 | emit Close(false); |
| 234 | } |
| 235 | |
| 236 | void DockHelp::showManual() |
| 237 | { |
| 238 | this->setVisible(true); |
| 239 | this->loadFile(docDir + "/index.html"); |
| 240 | } |
| 241 | |
| 242 | |
| 243 | void DockHelp::showGen() |
| 244 | { |
| 245 | this->setVisible(true); |
| 246 | this->loadFile(docDir + "/ScoreGenRef.html"); |
| 247 | } |
| 248 | |
| 249 | void DockHelp::showOverview() |
| 250 | { |
| 251 | this->setVisible(true); |
| 252 | this->loadFile(docDir + "/PartOpcodesOverview.html"); |
| 253 | } |
| 254 | |
| 255 | void DockHelp::showOpcodeQuickRef() |
| 256 | { |
| 257 | this->setVisible(true); |
| 258 | this->loadFile(docDir + "/MiscQuickref.html"); |
| 259 | } |
| 260 | |
| 261 | void DockHelp::browseBack() |
| 262 | { |
| 263 | ui->text->backward(); |
| 264 | } |
| 265 | |
| 266 | void DockHelp::browseForward() |
| 267 | { |
| 268 | ui->text->forward(); |
| 269 | } |
| 270 | |
| 271 | void DockHelp::followLink(QUrl url) |
| 272 | { |
| 273 | if (url.host() == "") { |
| 274 | // Will not follow external links for safety, only local files |
| 275 | if (url.toString().endsWith(".csd")) { |
| 276 | QString csdFile = url.toLocalFile().isEmpty() ? url.toString() : url.toLocalFile(); // necessary for windows 10 |
| 277 | emit openManualExample(csdFile); |
| 278 | } |
| 279 | else { |
| 280 | if (!url.toString().endsWith("indexframes.html") ) { |
| 281 | ui->text->setSource(url); |
| 282 | } |
| 283 | else { // Don't do anything with frames version... |
| 284 | // This could be fixed using the WebKit rendering engine |
| 285 | QMessageBox::warning(this, tr("CsoundQt"), |
| 286 | tr("Frames version only available in external browser.")); |
| 287 | emit requestExternalBrowser(url); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | else { |
| 292 | // QMessageBox::warning(this, tr("CsoundQt"), |
| 293 | // tr("External links can't be followed in help browser.")); |
| 294 | emit requestExternalBrowser(url); // connected to CsoundQt::openInExternalBrowser |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | void DockHelp::copy() |
| 299 | { |
| 300 | ui->text->copy(); |
| 301 | } |
| 302 | |
| 303 | void DockHelp::onTextChanged() |
| 304 | { |
| 305 | QTextCursor tmpCursor = ui->text->textCursor(); |
| 306 | tmpCursor.setPosition(ui->text->textCursor().selectionStart()); |
| 307 | ui->text->setTextCursor(tmpCursor); |
| 308 | findFlags &= 6; // first bit (FindBackward) to zero |
| 309 | findText(ui->findLine->text()); |
| 310 | } |
| 311 | |
| 312 | void DockHelp::onReturnPressed() |
| 313 | { |
| 314 | findFlags &= 6; // first bit (FindBackward) to zero |
| 315 | findText(ui->findLine->text()); |
| 316 | } |
| 317 | |
| 318 | void DockHelp::onNextButtonPressed() |
| 319 | { |
| 320 | findFlags &= 6; // first bit to zero |
| 321 | findText(ui->findLine->text()); |
| 322 | } |
| 323 | |
| 324 | void DockHelp::onPreviousButtonPressed() |
| 325 | { |
| 326 | findFlags |= QTextDocument::FindBackward; |
| 327 | findText(ui->findLine->text()); |
| 328 | } |
| 329 | |
| 330 | |
| 331 | void DockHelp::onCaseBoxChanged(int value) |
| 332 | { |
| 333 | if (value) |
| 334 | findFlags |= QTextDocument::FindCaseSensitively; |
| 335 | else |
| 336 | findFlags &= 5; // set 2 bit to 0 |
| 337 | } |
| 338 | |
| 339 | void DockHelp::onWholeWordBoxChanged(int value) |
| 340 | { |
| 341 | if (value) |
| 342 | findFlags |= QTextDocument::FindWholeWords; |
| 343 | else |
| 344 | findFlags &= 3; // set 3rd bit to 0 |
| 345 | } |
| 346 | |
| 347 | void DockHelp::findText(QString expr) |
| 348 | { |
| 349 | QTextCursor tmpCursor = ui->text->textCursor(); |
| 350 | if (!ui->text->find(expr,findFlags)) { // if not found, try from start |
| 351 | ui->text->moveCursor(QTextCursor::Start); |
| 352 | if (!ui->text->find(ui->findLine->text(),findFlags)) { |
| 353 | ui->text->setTextCursor(tmpCursor); // if not found at all, restore position |
| 354 | } |
| 355 | } else { |
| 356 | int cursorY = ui->text->cursorRect().top(); |
| 357 | QScrollBar *vbar = ui->text->verticalScrollBar(); |
| 358 | vbar->setValue(vbar->value() + cursorY); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | void DockHelp::resizeEvent(QResizeEvent *e) |
| 363 | { |
| 364 | QDockWidget::resizeEvent(e); |
| 365 | // ui->backButton->move(frameGeometry().width()/2-25, 0); |
| 366 | // ui->forwardButton->move(frameGeometry().width()/2, 0); |
| 367 | } |
| 368 | |
| 369 | |
| 370 | void DockHelp::focusText() { |
| 371 | ui->text->setFocus(); |
| 372 | } |
| 373 | |
| 374 | void DockHelp::keyPressEvent(QKeyEvent *event) { |
| 375 | if(event->key() == Qt::Key_Escape) { |
| 376 | toggleFindBarVisible(false); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | void DockHelp::toggleFindBarVisible(bool show) { |
| 381 | ui->findLine->setVisible(show); |
| 382 | ui->label->setVisible(show); |
| 383 | ui->caseBox->setVisible(show); |
| 384 | ui->wholeWordBox->setVisible(show); |
| 385 | ui->nextFindButton->setVisible(show); |
| 386 | ui->previousFindButton->setVisible(show); |
| 387 | if(show) { |
| 388 | ui->findLine->setFocus(); |
| 389 | } |
| 390 | } |