File indexing completed on 2024-10-06 13:33:57

0001 /**
0002  * SPDX-FileCopyrightText: (C) 2003 Sébastien Laoût <slaout@linux62.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef TOOLS_H
0008 #define TOOLS_H
0009 
0010 #include "basket_export.h"
0011 
0012 #include <QtCore/QVector>
0013 
0014 class State;
0015 class QColor;
0016 class QFont;
0017 class QMimeData;
0018 class QObject;
0019 class QPixmap;
0020 class QString;
0021 class QStringList;
0022 class QTime;
0023 class QTextDocument;
0024 
0025 class HTMLExporter;
0026 
0027 class StopWatch
0028 {
0029 public:
0030     static void start(int id);
0031     static void check(int id);
0032 
0033 private:
0034     static QVector<QTime> starts;
0035     static QVector<double> totals;
0036     static QVector<uint> counts;
0037 };
0038 
0039 /** Some useful functions for that application.
0040  * @author Sébastien Laoût
0041  */
0042 namespace Tools
0043 {
0044 // Text <-> HTML Conversions and Tools:
0045 BASKET_EXPORT QString textToHTML(const QString &text);
0046 BASKET_EXPORT QString textToHTMLWithoutP(const QString &text);
0047 BASKET_EXPORT QString htmlToParagraph(const QString &html);
0048 BASKET_EXPORT QString htmlToText(const QString &html);
0049 
0050 //Discards styles of tags applied to the note
0051 BASKET_EXPORT QString textDocumentToMinimalHTML(QTextDocument *document); //!< Avoid unneeded spans and style attributes
0052 
0053 BASKET_EXPORT QString detectURLs(const QString &text);
0054 BASKET_EXPORT QString cssFontDefinition(const QFont &font, bool onlyFontFamily = false);
0055 
0056 // Cross Reference tools:
0057 BASKET_EXPORT QString detectCrossReferences(const QString &text, bool userLink = false, HTMLExporter *exporter = nullptr);
0058 
0059 /**
0060  * @param text Plain text to detect tags by their corresponding text equivalent
0061  * @param prefixLength Length of a chunk that contains all recognized tags
0062  * @return detected tags
0063  */
0064 QList<State*> detectTags(const QString& text, int& prefixLength);
0065 
0066 // private functions:
0067 //Returns an empty string in case of failure
0068 BASKET_EXPORT QString crossReferenceForBasket(const QStringList& linkParts);
0069 
0070 //Returns an empty string in case of failure
0071 BASKET_EXPORT QString crossReferenceForHtml(const QStringList& linkParts, HTMLExporter *exporter);
0072 
0073 //Returns an empty string in case of failure
0074 BASKET_EXPORT QString crossReferenceForConversion(const QStringList& linkParts);
0075 
0076 // String Manipulations:
0077 BASKET_EXPORT QString stripEndWhiteSpaces(const QString &string);
0078 BASKET_EXPORT QString makeStandardCaption(const QString &userCaption); //!< Replacement for KDialog::makeStandardCaption
0079 
0080 // Pixmap Manipulations:
0081 /** @Return the CSS color name for the given @p colorHex in #rrggbb format, or empty string if none matches
0082  */
0083 BASKET_EXPORT QString cssColorName(const QString &colorHex);
0084 /** @Return true if it is a Web color
0085  */
0086 BASKET_EXPORT bool isWebColor(const QColor &color);
0087 /** @Return a color that is 50% of @p color1 and 50% of @p color2.
0088  */
0089 BASKET_EXPORT QColor mixColor(const QColor &color1, const QColor &color2, const float ratio = 1);
0090 /** @Return true if the color is too dark to be darkened one more time.
0091  */
0092 BASKET_EXPORT bool tooDark(const QColor &color);
0093 /** Make sure the @p pixmap is of the size (@p width, @p height) and @return a pixmap of this size.
0094  * If @p height <= 0, then width will be used to make the picture square.
0095  */
0096 BASKET_EXPORT QPixmap normalizePixmap(const QPixmap &pixmap, int width, int height = 0);
0097 /** @Return the pixmap @p source with depth*deltaX transparent pixels added to the left.\n
0098  * If @p deltaX is <= 0, then an indent delta is computed depending on the @p source width.
0099  */
0100 BASKET_EXPORT QPixmap indentPixmap(const QPixmap &source, int depth, int deltaX = 0);
0101 
0102 // File and Folder Manipulations:
0103 /** Delete the folder @p folderOrFile recursively (to remove sub-folders and child files too).
0104  */
0105 BASKET_EXPORT void deleteRecursively(const QString &folderOrFile);
0106 /** Trash the folder @p folderOrFile recursively (to move sub-folders and child files to the Trash, too).
0107  */
0108 BASKET_EXPORT void trashRecursively(const QString &folderOrFile);
0109 /** Delete the metadata of file or folder @p folderOrFile from Nepomuk, recursively.
0110  */
0111 BASKET_EXPORT void deleteMetadataRecursively(const QString &folderOrFile);
0112 /** @Return a new filename that doesn't already exist in @p destFolder.
0113  * If @p wantedName already exist in @p destFolder, a dash and a number will be added before the extension.
0114  * Id there were already such a number in @p wantedName, it is incremented until a free filename is found.
0115  */
0116 BASKET_EXPORT QString fileNameForNewFile(const QString &wantedName, const QString &destFolder);
0117 
0118 //! @returns Total size in bytes of all files and subdirectories
0119 BASKET_EXPORT qint64 computeSizeRecursively(const QString &path);
0120 
0121 // Other:
0122 // void iconForURL(const QUrl &url);
0123 /** @Return true if the source is from a file cutting in Konqueror.
0124  * @Return false if it was just a copy or if it was a drag.
0125  */
0126 BASKET_EXPORT bool isAFileCut(const QMimeData *source);
0127 
0128 /// Implementation of system encoding detection from KDE 4
0129 BASKET_EXPORT QByteArray systemCodeset();
0130 
0131 // Debug
0132 BASKET_EXPORT void printChildren(QObject *parent);
0133 }
0134 
0135 #endif // TOOLS_H