• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

blogilo

  • sources
  • kde-4.14
  • kdepim
  • blogilo
  • src
  • composer
blogilocomposereditor.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2013-2015 Montel Laurent <montel@kde.org>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 
19 */
20 
21 #include "blogilocomposereditor.h"
22 
23 #include "bilbomedia.h"
24 #include "global.h"
25 
26 #include <KDebug>
27 #include <KMimeType>
28 #include <KAction>
29 #include <KLocale>
30 
31 #include <QWebElementCollection>
32 #include <QWebFrame>
33 
34 BlogiloComposerEditor::BlogiloComposerEditor(BlogiloComposerView *view, QWidget *parent)
35  : ComposerEditorNG::ComposerEditor(view,parent),
36  readOnly(false)
37 {
38  QList<ComposerEditorNG::ComposerView::ComposerViewAction> lstActions;
39  lstActions<<ComposerEditorNG::ComposerView::Separator;
40  lstActions<<ComposerEditorNG::ComposerView::Bold;
41  lstActions<<ComposerEditorNG::ComposerView::Italic;
42  lstActions<<ComposerEditorNG::ComposerView::Underline;
43  lstActions<<ComposerEditorNG::ComposerView::StrikeOut;
44  lstActions<<ComposerEditorNG::ComposerView::Separator;
45  lstActions<<ComposerEditorNG::ComposerView::FormatType;
46  lstActions<<ComposerEditorNG::ComposerView::FontSize;
47  lstActions<<ComposerEditorNG::ComposerView::TextForegroundColor;
48  lstActions<<ComposerEditorNG::ComposerView::FormatReset;
49  lstActions<<ComposerEditorNG::ComposerView::BlockQuote;
50  lstActions<<ComposerEditorNG::ComposerView::Separator;
51  lstActions<<ComposerEditorNG::ComposerView::InsertLink;
52  lstActions<<ComposerEditorNG::ComposerView::InsertImage;
53  lstActions<<ComposerEditorNG::ComposerView::Separator;
54  lstActions<<ComposerEditorNG::ComposerView::AlignLeft;
55  lstActions<<ComposerEditorNG::ComposerView::AlignCenter;
56  lstActions<<ComposerEditorNG::ComposerView::AlignRight;
57  lstActions<<ComposerEditorNG::ComposerView::AlignJustify;
58  lstActions<<ComposerEditorNG::ComposerView::Separator;
59  lstActions<<ComposerEditorNG::ComposerView::DirectionRtl;
60  lstActions<<ComposerEditorNG::ComposerView::OrderedList;
61  lstActions<<ComposerEditorNG::ComposerView::UnorderedList;
62  lstActions<<ComposerEditorNG::ComposerView::Separator;
63  lstActions<<ComposerEditorNG::ComposerView::InsertTable;
64 
65 
66  QList<ComposerEditorNG::ComposerView::ComposerViewAction> toolBarActions;
67  toolBarActions<<lstActions;
68 
69  QList<ComposerEditorNG::ComposerView::ComposerViewAction> lstActionsFormat;
70  lstActionsFormat<<ComposerEditorNG::ComposerView::SubScript;
71  lstActionsFormat<<ComposerEditorNG::ComposerView::SuperScript;
72  lstActionsFormat<<ComposerEditorNG::ComposerView::Separator;
73  lstActionsFormat<<ComposerEditorNG::ComposerView::ListIndent;
74  lstActionsFormat<<ComposerEditorNG::ComposerView::ListDedent;
75  lstActionsFormat<<ComposerEditorNG::ComposerView::Separator;
76  lstActionsFormat<<ComposerEditorNG::ComposerView::TextBackgroundColor;
77 
78  //Create all actions first (before to add to toolbar)
79  createActions(lstActions<<ComposerEditorNG::ComposerView::PasteWithoutFormatting<<lstActionsFormat);
80 
81  KToolBar *mainToolBar = createToolBar(toolBarActions);
82 
83 
84  mActSplitPost = new KAction( KIcon( QLatin1String("insert-more-mark") ), i18n( "Split text" ), this );
85  connect( mActSplitPost, SIGNAL(triggered(bool)), this, SLOT(slotAddPostSplitter()) );
86  addActionInToolBar(mActSplitPost, mainToolBar);
87 
88  mActCode = new KAction( KIcon( QLatin1String("format-text-code") ), i18nc( "Sets text font to code style",
89  "Code" ), this );
90  connect( mActCode, SIGNAL(triggered(bool)), this, SLOT(slotToggleCode(bool)) );
91  addActionInToolBar(mActCode, mainToolBar);
92 
93  createToolBar(lstActionsFormat);
94 }
95 
96 BlogiloComposerEditor::~BlogiloComposerEditor()
97 {
98 
99 }
100 
101 void BlogiloComposerEditor::setReadOnly ( bool _readOnly )
102 {
103  if (readOnly != _readOnly) {
104  readOnly = _readOnly;
105  view()->evaluateJavascript( QString::fromLatin1( "setReadOnly(%1)" ).arg ( readOnly ? QLatin1String("true") : QLatin1String("false") ) );
106  }
107 }
108 
109 QList< BilboMedia* > BlogiloComposerEditor::getLocalImages()
110 {
111  kDebug();
112  QList< BilboMedia* > list;
113  QWebElementCollection images = view()->page()->mainFrame()->findAllElements(QLatin1String("img"));
114  Q_FOREACH (const QWebElement& elm, images){
115  if (elm.attribute(QLatin1String("src")).startsWith(QLatin1String("file://"))){
116  // kDebug()<<elm.toOuterXml();
117  BilboMedia* media = new BilboMedia(this);
118  KUrl mediaUrl (elm.attribute(QLatin1String("src")));
119  media->setLocalUrl( mediaUrl );
120  media->setMimeType( KMimeType::findByUrl( mediaUrl, 0, true )->name() );
121  media->setName(mediaUrl.fileName());
122  media->setBlogId(__currentBlogId);
123  list.append(media);
124  }
125  }
126  return list;
127 }
128 
129 void BlogiloComposerEditor::replaceImageSrc(const QString& src, const QString& dest)
130 {
131  const QString cmd = QString::fromLatin1("replaceImageSrc('%1','%2')").arg(src).arg(dest);
132  view()->evaluateJavascript(cmd);
133 }
134 
135 void BlogiloComposerEditor::slotAddPostSplitter()
136 {
137  execCommand(QLatin1String("insertHTML"), QLatin1String("<hr/><!--split-->"));
138 }
139 
140 void BlogiloComposerEditor::slotToggleCode(bool )
141 {
142  const QString selection = view()->selectedText();
143  if(selection.isEmpty())
144  return;
145  const QString html = QString::fromLatin1( "<code>%1</code>" ).arg ( selection );
146  execCommand(QLatin1String("insertHtml"), html);
147 }
148 
149 #if 0
150 QString TextEditor::getHtml() const
151 {
152  QString html = const_cast<TextEditor*>( this )->evaluateJavaScript( "getHtml()", false ).toString();
153  html.remove(" xmlns=\"http://www.w3.org/1999/xhtml\"", Qt::CaseInsensitive);
154  return html;
155 }
156 #endif
157 
158 void BlogiloComposerEditor::startEditing()
159 {
160  static_cast<BlogiloComposerView*>(view())->startEditing();
161 }
162 
163 void BlogiloComposerEditor::execCommand ( const QString &cmd )
164 {
165  QWebFrame *frame = view()->page()->mainFrame();
166  const QString js = QString::fromLatin1( "document.execCommand(\"%1\", false, null)" ).arg ( cmd );
167  frame->evaluateJavaScript ( js );
168 }
169 
170 void BlogiloComposerEditor::execCommand( const QString &cmd, const QString &arg )
171 {
172  QWebFrame *frame = view()->page()->mainFrame();
173  const QString js = QString::fromLatin1( "document.execCommand(\"%1\", false, \"%2\")" ).arg ( cmd ).arg ( arg );
174  frame->evaluateJavaScript ( js );
175 }
176 
177 void BlogiloComposerEditor::insertShortUrl(const QString &url)
178 {
179  QString html = QString::fromLatin1( "<a href=\'%1\'>%1</a>").arg(url);
180  execCommand(QLatin1String("insertHtml"), html);
181 }
182 
183 
184 
QWidget
QWebFrame
BlogiloComposerEditor::~BlogiloComposerEditor
~BlogiloComposerEditor()
Definition: blogilocomposereditor.cpp:96
BlogiloComposerEditor::setReadOnly
void setReadOnly(bool _readOnly)
Definition: blogilocomposereditor.cpp:101
QString::remove
QString & remove(int position, int n)
BlogiloComposerEditor::replaceImageSrc
void replaceImageSrc(const QString &src, const QString &dest)
Definition: blogilocomposereditor.cpp:129
global.h
BilboMedia::setLocalUrl
void setLocalUrl(const KUrl &url)
Definition: bilbomedia.cpp:100
BilboMedia::setMimeType
void setMimeType(const QString &type)
Definition: bilbomedia.cpp:120
BlogiloComposerEditor::insertShortUrl
void insertShortUrl(const QString &url)
Definition: blogilocomposereditor.cpp:177
QList::append
void append(const T &value)
__currentBlogId
int __currentBlogId
Global variables.
Definition: global.cpp:29
QWebElement
blogilocomposereditor.h
QString::isEmpty
bool isEmpty() const
QWebPage::mainFrame
QWebFrame * mainFrame() const
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
bilbomedia.h
BlogiloComposerView
Definition: blogilocomposerview.h:28
QString
QList
QWebFrame::evaluateJavaScript
QVariant evaluateJavaScript(const QString &scriptSource)
QWebFrame::page
QWebPage * page() const
QWebElement::attribute
QString attribute(const QString &name, const QString &defaultValue) const
BilboMedia::setName
void setName(const QString &name)
Definition: bilbomedia.cpp:130
BilboMedia
Definition: bilbomedia.h:38
QLatin1String
BlogiloComposerEditor::startEditing
void startEditing()
Definition: blogilocomposereditor.cpp:158
QString::fromLatin1
QString fromLatin1(const char *str, int size)
BilboMedia::setBlogId
void setBlogId(const int blog_id)
Definition: bilbomedia.cpp:70
BlogiloComposerEditor::BlogiloComposerEditor
BlogiloComposerEditor(BlogiloComposerView *view, QWidget *parent)
Definition: blogilocomposereditor.cpp:34
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
BlogiloComposerEditor::getLocalImages
QList< BilboMedia * > getLocalImages()
Definition: blogilocomposereditor.cpp:109
QWebElementCollection
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:16 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

blogilo

Skip menu "blogilo"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal