/src/gui/browserWidgets/urleditor.cpp

https://github.com/robert7/nixnote2 · C++ · 131 lines · 83 code · 27 blank · 21 comment · 16 complexity · 5ee37365748bda879b2f4aebcdc674a4 MD5 · raw file

  1. /*********************************************************************************
  2. NixNote - An open-source client for the Evernote service.
  3. Copyright (C) 2013 Randy Baumgarte
  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. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. ***********************************************************************************/
  16. #include "urleditor.h"
  17. #include "src/global.h"
  18. #include "src/sql/notetable.h"
  19. #include <QDesktopServices>
  20. extern Global global;
  21. UrlEditor::UrlEditor(QWidget *parent) :
  22. QLineEdit(parent)
  23. {
  24. // Setup the note title editor
  25. QPalette pal;
  26. pal.setColor(backgroundRole(), QPalette::Base);
  27. setPalette(pal);
  28. this->setFont(global.getGuiFont(font()));
  29. inactiveColor = global.getUrlEditorInactiveStyle();
  30. activeColor = global.getUrlEditorActiveStyle();
  31. this->setCursor(Qt::PointingHandCursor);
  32. this->setStyleSheet(inactiveColor);
  33. this->setPlaceholderText(tr("Click to set source URL..."));
  34. connect(this, SIGNAL(textChanged(QString)), this, SLOT(textModified(QString)));
  35. hide();
  36. QString css = global.getThemeCss("urlEditorCss");
  37. if (css!="")
  38. this->setStyleSheet(css);
  39. }
  40. void UrlEditor::setActiveColor() {
  41. setStyleSheet(activeColor);
  42. }
  43. void UrlEditor::setUrl(qint32 lid, QString text) {
  44. currentLid = lid;
  45. blockSignals(true);
  46. setText(text);
  47. blockSignals(false);
  48. initialText = text;
  49. priorText = text;
  50. }
  51. void UrlEditor::focusInEvent(QFocusEvent *e)
  52. {
  53. QLineEdit::focusInEvent(e);
  54. setStyleSheet(activeColor);
  55. this->setCursor(Qt::ArrowCursor);
  56. emit(focussed(true));
  57. }
  58. void UrlEditor::focusOutEvent(QFocusEvent *e)
  59. {
  60. QLineEdit::focusOutEvent(e);
  61. this->setCursor(Qt::PointingHandCursor);
  62. setStyleSheet(inactiveColor);
  63. textModified(text());
  64. emit(focussed(false));
  65. }
  66. void UrlEditor::textModified(QString text) {
  67. if (hasFocus())
  68. return;
  69. this->blockSignals(true);
  70. NoteTable noteTable(global.db);
  71. noteTable.updateUrl(currentLid, text, true);
  72. if (text.trimmed() == "" && !hasFocus() && priorText.trimmed() == text.trimmed())
  73. return;
  74. else
  75. setText(text);
  76. this->blockSignals(false);
  77. if (text.trimmed() != initialText.trimmed() || priorText.trimmed() != text.trimmed())
  78. emit(textUpdated());
  79. priorText = text;
  80. }
  81. QString UrlEditor::getText() {
  82. return text();
  83. }
  84. // Listen for mouse press events. This tells us if
  85. // we need to open based on a middle click.
  86. void UrlEditor::mouseReleaseEvent(QMouseEvent *e) {
  87. if ( e->button() == Qt::MidButton) {
  88. if (text().trimmed() != "") {
  89. QString url = text().trimmed();
  90. if (!url.toLower().startsWith("http://") &&
  91. !url.toLower().startsWith("https://") &&
  92. !url.toLower().startsWith("mailto://") &&
  93. !url.toLower().startsWith("mailto:") &&
  94. !url.toLower().startsWith("ftp://") &&
  95. !url.toLower().startsWith("file://"))
  96. url = "http://"+url;
  97. QDesktopServices::openUrl(QUrl(url));
  98. }
  99. } else
  100. QLineEdit::mouseReleaseEvent(e);
  101. }