1 /*************************************************************************** 2 * Copyright (C) 2004-2021 by Pere Constans 3 * constans@molspaces.com 4 * cb2Bib version 2.0.1. Licensed under the GNU GPL version 3. 5 * See the LICENSE file that comes with this distribution. 6 ***************************************************************************/ 7 #ifndef C2BUTILS_H 8 #define C2BUTILS_H 9 10 #include <cb2bib_utilities.h> 11 12 #include "dialog.h" 13 14 #include <QAction> 15 #include <QTextDocument> 16 #include <QUrl> 17 18 /** 19 General cb2Bib Utils: GUI related utilities 20 */ 21 namespace c2bUtils 22 { 23 24 extern bool openFile(const QString& fn, QWidget* w = 0); 25 26 inline void displayHelp(const QString& url) 27 { 28 openFile(url); 29 } 30 31 inline QUrl stringToUrl(const QString& fn) 32 { 33 // Avoid cross-platform pitfalls regarding string to url conversions 34 // Avoid possible issues regarding QUrl and local files not starting with 'file://' 35 if (QFileInfo::exists(fn)) 36 return QUrl::fromLocalFile(fn); 37 else 38 return QUrl(fn, QUrl::TolerantMode); 39 } 40 41 inline QString fromHtmlString(const QString& str, const bool addMetadata = false) 42 { 43 QTextDocument converter; 44 converter.setHtml(str); 45 if (addMetadata) 46 { 47 const QString md(converter.metaInformation(QTextDocument::DocumentTitle).trimmed()); 48 if (md.isEmpty()) 49 return converter.toPlainText(); 50 else 51 return "Document Title: " + md + "\n\n" + converter.toPlainText(); 52 } 53 else 54 return converter.toPlainText(); 55 } 56 57 inline void addSeparator(QWidget* w) 58 { 59 // Adds separator to widget w 60 QAction* action = new QAction(w); 61 action->setSeparator(true); 62 w->addAction(action); 63 } 64 65 inline void setWidgetOnTop(QWidget* w) 66 { 67 if (w) 68 { 69 if (w->isHidden()) 70 w->show(); 71 if (w->isMinimized()) 72 w->showNormal(); 73 w->raise(); 74 w->activateWindow(); 75 } 76 } 77 78 } // namespace c2bUtils 79 80 #endif