commit 89f9bc2f3f66fdf98bcf529e90639385b0a0cef2 Author: Rick Blommers Date: Fri Apr 19 08:26:54 2013 +0200 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ca40fa6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.DS_Store +*.pro.user +/*build* \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..27c3147 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,29 @@ +MIT License +=========== + +Copyright 2013 - [Reliable Bits Software by Blommers IT](http://blommersit.nl). All Rights Reserved. +Author [Rick Blommers](mailto:rick@blommersit.nl) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, +and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO +THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS +OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + +Font Awesome License +==================== + +[https://github.com/FortAwesome/Font-Awesome](https://github.com/FortAwesome/Font-Awesome) + +The Font Awesome font is licensed under the SIL Open Font License - [http://scripts.sil.org/OFL](http://scripts.sil.org/OFL) +The Font Awesome pictograms are licensed under the CC BY 3.0 License - [http://creativecommons.org/licenses/by/3.0/](http://creativecommons.org/licenses/by/3.0/) +"Font Awesome by Dave Gandy - http://fortawesome.github.com/Font-Awesome" + + diff --git a/QtAwesome/QtAwesome.cpp b/QtAwesome/QtAwesome.cpp new file mode 100644 index 0000000..85186db --- /dev/null +++ b/QtAwesome/QtAwesome.cpp @@ -0,0 +1,543 @@ +/** + * QtAwesome - use font-awesome (or other font icons) in your c++ / Qt Application + * + * MIT License: + * + * Copyright 2013 - Reliable Bits Software by Blommers IT. All Rights Reserved. + * Author Rick Blommers + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS + * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * I would appriciate it if you let me know which projects you are using this for + */ + +#include "QtAwesome.h" + +#include +#include +#include + + +/// The font-awesome icon painter +class QtAwesomeCharIconPainter: public QtAwesomeIconPainter +{ +public: + virtual void paint( QtAwesome* awesome, QPainter* painter, const QRect& rect, QIcon::Mode mode, QIcon::State state, const QVariantMap& options ) + { + Q_UNUSED(mode); + Q_UNUSED(state); + Q_UNUSED(options); + + painter->save(); + + // set the correct color + QColor color = options.value("color").value(); + if( mode == QIcon::Disabled ) { + color = options.value("color-disabled").value(); + } else if( mode == QIcon::Active ) { + color = options.value("color-active").value(); + } else if( mode == QIcon::Selected ) { + color = options.value("color-selected").value(); + } + painter->setPen(color); + + // add some 'padding' around the icon + int drawSize = qRound(rect.height()*options.value("scale-factor").toFloat()); + QFont font(awesome->fontName()); + font.setPixelSize(drawSize); // use pixel size + + painter->setFont( font ); + painter->drawText( rect, options.value("text").toString(), QTextOption( Qt::AlignCenter|Qt::AlignVCenter ) ); + painter->restore(); + } + +}; + + +//--------------------------------------------------------------------------------------- + + +/// The painter icon engine. +class QtAwesomeIconPainterIconEngine : public QIconEngine +{ + +public: + + QtAwesomeIconPainterIconEngine( QtAwesome* awesome, QtAwesomeIconPainter* painter, const QVariantMap& options ) + : awesomeRef_(awesome) + , iconPainterRef_(painter) + , options_(options) + { + } + + virtual ~QtAwesomeIconPainterIconEngine() {} + + QtAwesomeIconPainterIconEngine* clone() const + { + return new QtAwesomeIconPainterIconEngine( awesomeRef_, iconPainterRef_, options_ ); + } + + virtual void paint(QPainter* painter, const QRect& rect, QIcon::Mode mode, QIcon::State state) + { + Q_UNUSED( mode ); + Q_UNUSED( state ); + iconPainterRef_->paint( awesomeRef_, painter, rect, mode, state, options_ ); + } + + virtual QPixmap pixmap(const QSize& size, QIcon::Mode mode, QIcon::State state) + { + QPixmap pm(size); + pm.fill( Qt::transparent ); // we need transparency + { + QPainter p(&pm); + paint(&p, QRect(QPoint(0,0),size), mode, state); + } + return pm; + } + +private: + + QtAwesome* awesomeRef_; ///< a reference to the QtAwesome instance + QtAwesomeIconPainter* iconPainterRef_; ///< a reference to the icon painter + QVariantMap options_; ///< the options for this icon painter +}; + + +//--------------------------------------------------------------------------------------- + +/// The default icon colors +QtAwesome::QtAwesome( QObject* parent ) + : QObject( parent ) + , namedCodepoints_() +{ + // initialize the default options + setDefaultOption( "color", QColor(50,50,50) ); + setDefaultOption( "color-disabled", QColor(70,70,70,60)); + setDefaultOption( "color-active", QColor(10,10,10)); + setDefaultOption( "color-selected", QColor(10,10,10)); + setDefaultOption( "scale-factor", 0.9 ); + + fontIconPainter_ = new QtAwesomeCharIconPainter(); + +} + + +QtAwesome::~QtAwesome() +{ + delete fontIconPainter_; +// delete errorIconPainter_; + qDeleteAll(painterMap_); +} + +/// initializes the QtAwesome icon factory with the given fontname +void QtAwesome::init(const QString& fontname) +{ + fontName_ = fontname; +} + + +/// a specialized init function so font-awesome is loaded and initialized +/// this method return true on success, it will return false if the fnot cannot be initialized +/// To initialize QtAwesome with font-awesome you need to call this method +bool QtAwesome::initFontAwesome( ) +{ + static int fontAwesomeFontId = -1; + + // only load font-awesome once + if( fontAwesomeFontId < 0 ) { + QFile res(":/fonts/fontawesome.ttf"); + if(!res.open(QIODevice::ReadOnly)) { + qDebug() << "Font awesome font could not be loaded!"; + return false; + } + QByteArray fontData( res.readAll() ); + res.close(); + + // fetch the given font + fontAwesomeFontId = QFontDatabase::addApplicationFontFromData(fontData); + } + + QStringList loadedFontFamilies = QFontDatabase::applicationFontFamilies(fontAwesomeFontId); + if( !loadedFontFamilies.empty() ) { + fontName_= loadedFontFamilies.at(0); + } else { + qDebug() << "Font awesome font is empty?!"; + fontAwesomeFontId = -1; // restore the font-awesome id + return false; + } + + // intialize the map + QHash& m = namedCodepoints_; + m.insert( "glass", icon_glass ); + m.insert( "music", icon_music ); + m.insert( "search", icon_search ); + m.insert( "envelope", icon_envelope ); + m.insert( "heart", icon_heart ); + m.insert( "star", icon_star ); + m.insert( "star-empty", icon_star_empty ); + m.insert( "user", icon_user ); + m.insert( "film", icon_film ); + m.insert( "th-large", icon_th_large ); + m.insert( "th", icon_th ); + m.insert( "th-list", icon_th_list ); + m.insert( "ok", icon_ok ); + m.insert( "remove", icon_remove ); + m.insert( "zoom-in", icon_zoom_in ); + + m.insert( "zoom-out", icon_zoom_out ); + m.insert( "off", icon_off ); + m.insert( "signal", icon_signal ); + m.insert( "cog", icon_cog ); + m.insert( "trash", icon_trash ); + m.insert( "home", icon_home ); + m.insert( "file", icon_file ); + m.insert( "time", icon_time ); + m.insert( "road", icon_road ); + m.insert( "download-alt", icon_download_alt ); + m.insert( "download", icon_download ); + m.insert( "upload", icon_upload ); + m.insert( "inbox", icon_inbox ); + m.insert( "play-circle", icon_play_circle ); + m.insert( "repeat", icon_repeat ); + + /* \f020 doesn't work in Safari. all shifted one down */ + + + m.insert( "refresh", icon_refresh ); + m.insert( "list-alt", icon_list_alt ); + m.insert( "lock", icon_lock ); + m.insert( "flag", icon_flag ); + m.insert( "headphones", icon_headphones ); + m.insert( "volume-off", icon_volume_off ); + m.insert( "volume-down", icon_volume_down ); + m.insert( "volume-up", icon_volume_up ); + m.insert( "qrcode", icon_qrcode ); + m.insert( "barcode", icon_barcode ); + m.insert( "tag", icon_tag ); + m.insert( "tags", icon_tags ); + m.insert( "book", icon_book ); + m.insert( "bookmark", icon_bookmark ); + m.insert( "print", icon_print ); + + m.insert( "camera", icon_camera ); + m.insert( "font", icon_font ); + m.insert( "bold", icon_bold ); + m.insert( "italic", icon_italic ); + m.insert( "text-height", icon_text_height ); + m.insert( "text-width", icon_text_width ); + m.insert( "align-left", icon_align_left ); + m.insert( "align-center", icon_align_center ); + m.insert( "align-right", icon_align_right ); + m.insert( "align-justify", icon_align_justify ); + m.insert( "list", icon_list ); + m.insert( "indent-left", icon_indent_left ); + m.insert( "indent-right", icon_indent_right ); + m.insert( "facetime-video", icon_facetime_video ); + m.insert( "picture", icon_picture ); + + m.insert( "pencil", icon_pencil ); + m.insert( "map-marker", icon_map_marker ); + m.insert( "adjust", icon_adjust ); + m.insert( "tint", icon_tint ); + m.insert( "edit", icon_edit ); + m.insert( "share", icon_share ); + m.insert( "check", icon_check ); + m.insert( "move", icon_move ); + m.insert( "step-backward", icon_step_backward ); + m.insert( "fast-backward", icon_fast_backward ); + m.insert( "backward", icon_backward ); + m.insert( "play", icon_play ); + m.insert( "pause", icon_pause ); + m.insert( "stop", icon_stop ); + m.insert( "forward", icon_forward ); + + m.insert( "fast-forward", icon_fast_forward ); + m.insert( "step-forward", icon_step_forward ); + m.insert( "eject", icon_eject ); + m.insert( "chevron-left", icon_chevron_left ); + m.insert( "chevron-right", icon_chevron_right ); + m.insert( "plus-sign", icon_plus_sign ); + m.insert( "minus-sign", icon_minus_sign ); + m.insert( "remove-sign", icon_remove_sign ); + m.insert( "ok-sign", icon_ok_sign ); + m.insert( "question-sign", icon_question_sign ); + m.insert( "info-sign", icon_info_sign ); + m.insert( "screenshot", icon_screenshot ); + m.insert( "remove-circle", icon_remove_circle ); + m.insert( "ok-circle", icon_ok_circle ); + m.insert( "ban-circle", icon_ban_circle ); + + m.insert( "arrow-left", icon_arrow_left ); + m.insert( "arrow-right", icon_arrow_right ); + m.insert( "arrow-up", icon_arrow_up ); + m.insert( "arrow-down", icon_arrow_down ); + m.insert( "share-alt", icon_share_alt ); + m.insert( "resize-full", icon_resize_full ); + m.insert( "resize-small", icon_resize_small ); + m.insert( "plus", icon_plus ); + m.insert( "minus", icon_minus ); + m.insert( "asterisk", icon_asterisk ); + m.insert( "exclamation-sign", icon_exclamation_sign ); + m.insert( "gift", icon_gift ); + m.insert( "leaf", icon_leaf ); + m.insert( "fire", icon_fire ); + m.insert( "eye-open", icon_eye_open ); + + m.insert( "eye-close", icon_eye_close ); + m.insert( "warning-sign", icon_warning_sign ); + m.insert( "plane", icon_plane ); + m.insert( "calendar", icon_calendar ); + m.insert( "random", icon_random ); + m.insert( "comment", icon_comment ); + m.insert( "magnet", icon_magnet ); + m.insert( "chevron-up", icon_chevron_up ); + m.insert( "chevron-down", icon_chevron_down ); + m.insert( "retweet", icon_retweet ); + m.insert( "shopping-cart", icon_shopping_cart ); + m.insert( "folder-close", icon_folder_close ); + m.insert( "folder-open", icon_folder_open ); + m.insert( "resize-vertical", icon_resize_vertical ); + m.insert( "resize-horizontal", icon_resize_horizontal ); + + m.insert( "bar-chart", icon_bar_chart ); + m.insert( "twitter-sign", icon_twitter_sign ); + m.insert( "facebook-sign", icon_facebook_sign ); + m.insert( "camera-retro", icon_camera_retro ); + m.insert( "key", icon_key ); + m.insert( "cogs", icon_cogs ); + m.insert( "comments", icon_comments ); + m.insert( "thumbs-up", icon_thumbs_up ); + m.insert( "thumbs-down", icon_thumbs_down ); + m.insert( "star-half", icon_star_half ); + m.insert( "heart-empty", icon_heart_empty ); + m.insert( "signout", icon_signout ); + m.insert( "linkedin-sign", icon_linkedin_sign ); + m.insert( "pushpin", icon_pushpin ); + m.insert( "external-link", icon_external_link ); + + m.insert( "signin", icon_signin ); + m.insert( "trophy", icon_trophy ); + m.insert( "github-sign", icon_github_sign ); + m.insert( "upload-alt", icon_upload_alt ); + m.insert( "lemon", icon_lemon ); + m.insert( "phone", icon_phone ); + m.insert( "check-empty", icon_check_empty ); + m.insert( "bookmark-empty", icon_bookmark_empty ); + m.insert( "phone-sign", icon_phone_sign ); + m.insert( "twitter", icon_twitter ); + m.insert( "facebook", icon_facebook ); + m.insert( "github", icon_github ); + m.insert( "unlock", icon_unlock ); + m.insert( "credit-card", icon_credit_card ); + m.insert( "rss", icon_rss ); + + m.insert( "hdd", icon_hdd ); + m.insert( "bullhorn", icon_bullhorn ); + m.insert( "bell", icon_bell ); + m.insert( "certificate", icon_certificate ); + m.insert( "hand-right", icon_hand_right ); + m.insert( "hand-left", icon_hand_left ); + m.insert( "hand-up", icon_hand_up ); + m.insert( "hand-down", icon_hand_down ); + m.insert( "circle-arrow-left", icon_circle_arrow_left ); + m.insert( "circle-arrow-right", icon_circle_arrow_right ); + m.insert( "circle-arrow-up", icon_circle_arrow_up ); + m.insert( "circle-arrow-down", icon_circle_arrow_down ); + m.insert( "globe", icon_globe ); + m.insert( "wrench", icon_wrench ); + m.insert( "tasks", icon_tasks ); + + m.insert( "filter", icon_filter ); + m.insert( "briefcase", icon_briefcase ); + m.insert( "fullscreen", icon_fullscreen ); + + m.insert( "group", icon_group ); + m.insert( "link", icon_link ); + m.insert( "cloud", icon_cloud ); + m.insert( "beaker", icon_beaker ); + m.insert( "cut", icon_cut ); + m.insert( "copy", icon_copy ); + m.insert( "paper-clip", icon_paper_clip ); + m.insert( "save", icon_save ); + m.insert( "sign-blank", icon_sign_blank ); + m.insert( "reorder", icon_reorder ); + m.insert( "list-ul", icon_list_ul ); + m.insert( "list-ol", icon_list_ol ); + m.insert( "strikethrough", icon_strikethrough ); + m.insert( "underline", icon_underline ); + m.insert( "table", icon_table ); + + m.insert( "magic", icon_magic ); + m.insert( "truck", icon_truck ); + m.insert( "pinterest", icon_pinterest ); + m.insert( "pinterest-sign", icon_pinterest_sign ); + m.insert( "google-plus-sign", icon_google_plus_sign ); + m.insert( "google-plus", icon_google_plus ); + m.insert( "money", icon_money ); + m.insert( "caret-down", icon_caret_down ); + m.insert( "caret-up", icon_caret_up ); + m.insert( "caret-left", icon_caret_left ); + m.insert( "caret-right", icon_caret_right ); + m.insert( "columns", icon_columns ); + m.insert( "sort", icon_sort ); + m.insert( "sort-down", icon_sort_down ); + m.insert( "sort-up", icon_sort_up ); + + m.insert( "envelope-alt", icon_envelope_alt ); + m.insert( "linkedin", icon_linkedin ); + m.insert( "undo", icon_undo ); + m.insert( "legal", icon_legal ); + m.insert( "dashboard", icon_dashboard ); + m.insert( "comment-alt", icon_comment_alt ); + m.insert( "comments-alt", icon_comments_alt ); + m.insert( "bolt", icon_bolt ); + m.insert( "sitemap", icon_sitemap ); + m.insert( "umbrella", icon_umbrella ); + m.insert( "paste", icon_paste ); + m.insert( "lightbulb", icon_lightbulb ); + m.insert( "exchange", icon_exchange ); + m.insert( "cloud-download", icon_cloud_download ); + m.insert( "cloud-upload", icon_cloud_upload ); + + m.insert( "user-md", icon_user_md ); + m.insert( "stethoscope", icon_stethoscope ); + m.insert( "suitcase", icon_suitcase ); + m.insert( "bell-alt", icon_bell_alt ); + m.insert( "coffee", icon_coffee ); + m.insert( "food", icon_food ); + m.insert( "file-alt", icon_file_alt ); + m.insert( "building", icon_building ); + m.insert( "hospital", icon_hospital ); + m.insert( "ambulance", icon_ambulance ); + m.insert( "medkit", icon_medkit ); + m.insert( "fighter-jet", icon_fighter_jet ); + m.insert( "beer", icon_beer ); + m.insert( "h-sign", icon_h_sign ); + m.insert( "plus-sign-alt", icon_plus_sign_alt ); + + m.insert( "double-angle-left", icon_double_angle_left ); + m.insert( "double-angle-right", icon_double_angle_right ); + m.insert( "double-angle-up", icon_double_angle_up ); + m.insert( "double-angle-down", icon_double_angle_down ); + m.insert( "angle-left", icon_angle_left ); + m.insert( "angle-right", icon_angle_right ); + m.insert( "angle-up", icon_angle_up ); + m.insert( "angle-down", icon_angle_down ); + m.insert( "desktop", icon_desktop ); + m.insert( "laptop", icon_laptop ); + m.insert( "tablet", icon_tablet ); + m.insert( "mobile-phone", icon_mobile_phone ); + m.insert( "circle-blank", icon_circle_blank ); + m.insert( "quote-left", icon_quote_left ); + m.insert( "quote-right", icon_quote_right ); + + m.insert( "spinner", icon_spinner ); + m.insert( "circle", icon_circle ); + m.insert( "reply", icon_reply ); + m.insert( "github-alt", icon_github_alt ); + m.insert( "folder-close-alt", icon_folder_close_alt ); + m.insert( "folder-open-alt", icon_folder_open_alt ); + return true; +} + +void QtAwesome::addNamedCodepoint( const QString& name, int codePoint) +{ + namedCodepoints_.insert( name, codePoint); +} + + +/// Sets a default option. These options are passed on to the icon painters +void QtAwesome::setDefaultOption(const QString& name, const QVariant& value) +{ + defaultOptions_.insert( name, value ); +} + + +/// Returns the default option for the given name +QVariant QtAwesome::defaultOption(const QString& name) +{ + return defaultOption( name ); +} + + +// internal helper method to merge to option amps +static QVariantMap mergeOptions( const QVariantMap& defaults, const QVariantMap& override ) +{ + QVariantMap result= defaults; + if( !override.isEmpty() ) { + QMapIterator itr(override); + while( itr.hasNext() ) { + itr.next(); + result.insert( itr.key(), itr.value() ); + } + } + return result; +} + + +/// Creates an icon with the given code-point +/// +/// awesome->icon( icon_group ) +/// +QIcon QtAwesome::icon(int character, const QVariantMap &options) +{ + // create a merged QVariantMap to have default options and icon-specific options + QVariantMap optionMap = mergeOptions( defaultOptions_, options ); + optionMap.insert("text", QString( QChar(character) ) ); + + QtAwesomeIconPainterIconEngine* engine = new QtAwesomeIconPainterIconEngine( this, fontIconPainter_, optionMap ); + return QIcon( engine ); +} + + + +/// Creates an icon with the given name +/// +/// You can use the icon names as defined on http://fortawesome.github.io/Font-Awesome/design.html withour the 'icon-' prefix +/// @param name the name of the icon +/// @param options extra option to pass to the icon renderer +QIcon QtAwesome::icon(const QString& name, const QVariantMap& options) +{ + // when it's a named codepoint + if( namedCodepoints_.count(name) ) { + return icon( namedCodepoints_.value(name), options ); + } + + + // create a merged QVariantMap to have default options and icon-specific options + QVariantMap optionMap = mergeOptions( defaultOptions_, options ); + + // this method first tries to retrieve the icon + QtAwesomeIconPainter* painter = painterMap_.value(name); + if( !painter ) { + return QIcon(); + } + + // Warning, when you use memoryleak detection. You should turn it of for the next call + // QIcon's placed in gui items are often cached and not deleted when my memory-leak detection checks for leaks. + // I'm not sure if it's a Qt bug or something I do wrong + QtAwesomeIconPainterIconEngine* engine = new QtAwesomeIconPainterIconEngine( this, painter, optionMap ); + return QIcon( engine ); +} + +/// Adds a named icon-painter to the QtAwesome icon map +/// As the name applies the ownership is passed over to QtAwesome +/// +/// @param name the name of the icon +/// @param painter the icon painter to add for this name +void QtAwesome::give(const QString& name, QtAwesomeIconPainter* painter) +{ + delete painterMap_.value( name ); // delete the old one + painterMap_.insert( name, painter ); +} + diff --git a/QtAwesome/QtAwesome.h b/QtAwesome/QtAwesome.h new file mode 100644 index 0000000..6589ee0 --- /dev/null +++ b/QtAwesome/QtAwesome.h @@ -0,0 +1,363 @@ +/** + * QtAwesome - use font-awesome (or other font icons) in your c++ / Qt Application + * + * MIT License: + * + * Copyright 2013 - Reliable Bits Software by Blommers IT. All Rights Reserved. + * Author Rick Blommers + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS + * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * I would appriciate it if you let me know which projects you are using this for. + */ + +#ifndef QTAWESOME_H +#define QTAWESOME_H + +#include +#include +#include +#include +#include + + +/// A list of all icon-names with the codepoint (unicode-value) on the right +/// You can use the names on the page http://fortawesome.github.io/Font-Awesome/design.html ( replace every dash '-' with an underscore '_') +enum QtFontAwesomeName { + icon_glass = 0xf000, + icon_music = 0xf001, + icon_search = 0xf002, + icon_envelope = 0xf003, + icon_heart = 0xf004, + icon_star = 0xf005, + icon_star_empty = 0xf006, + icon_user = 0xf007, + icon_film = 0xf008, + icon_th_large = 0xf009, + icon_th = 0xf00a, + icon_th_list = 0xf00b, + icon_ok = 0xf00c, + icon_remove = 0xf00d, + icon_zoom_in = 0xf00e, + + icon_zoom_out = 0xf010, + icon_off = 0xf011, + icon_signal = 0xf012, + icon_cog = 0xf013, + icon_trash = 0xf014, + icon_home = 0xf015, + icon_file = 0xf016, + icon_time = 0xf017, + icon_road = 0xf018, + icon_download_alt = 0xf019, + icon_download = 0xf01a, + icon_upload = 0xf01b, + icon_inbox = 0xf01c, + icon_play_circle = 0xf01d, + icon_repeat = 0xf01e, + + /* \f020 doesn't work in Safari. all shifted one down */ + icon_refresh = 0xf021, + icon_list_alt = 0xf022, + icon_lock = 0xf023, + icon_flag = 0xf024, + icon_headphones = 0xf025, + icon_volume_off = 0xf026, + icon_volume_down = 0xf027, + icon_volume_up = 0xf028, + icon_qrcode = 0xf029, + icon_barcode = 0xf02a, + icon_tag = 0xf02b, + icon_tags = 0xf02c, + icon_book = 0xf02d, + icon_bookmark = 0xf02e, + icon_print = 0xf02f, + + icon_camera = 0xf030, + icon_font = 0xf031, + icon_bold = 0xf032, + icon_italic = 0xf033, + icon_text_height = 0xf034, + icon_text_width = 0xf035, + icon_align_left = 0xf036, + icon_align_center = 0xf037, + icon_align_right = 0xf038, + icon_align_justify = 0xf039, + icon_list = 0xf03a, + icon_indent_left = 0xf03b, + icon_indent_right = 0xf03c, + icon_facetime_video = 0xf03d, + icon_picture = 0xf03e, + + icon_pencil = 0xf040, + icon_map_marker = 0xf041, + icon_adjust = 0xf042, + icon_tint = 0xf043, + icon_edit = 0xf044, + icon_share = 0xf045, + icon_check = 0xf046, + icon_move = 0xf047, + icon_step_backward = 0xf048, + icon_fast_backward = 0xf049, + icon_backward = 0xf04a, + icon_play = 0xf04b, + icon_pause = 0xf04c, + icon_stop = 0xf04d, + icon_forward = 0xf04e, + + icon_fast_forward = 0xf050, + icon_step_forward = 0xf051, + icon_eject = 0xf052, + icon_chevron_left = 0xf053, + icon_chevron_right = 0xf054, + icon_plus_sign = 0xf055, + icon_minus_sign = 0xf056, + icon_remove_sign = 0xf057, + icon_ok_sign = 0xf058, + icon_question_sign = 0xf059, + icon_info_sign = 0xf05a, + icon_screenshot = 0xf05b, + icon_remove_circle = 0xf05c, + icon_ok_circle = 0xf05d, + icon_ban_circle = 0xf05e, + + icon_arrow_left = 0xf060, + icon_arrow_right = 0xf061, + icon_arrow_up = 0xf062, + icon_arrow_down = 0xf063, + icon_share_alt = 0xf064, + icon_resize_full = 0xf065, + icon_resize_small = 0xf066, + icon_plus = 0xf067, + icon_minus = 0xf068, + icon_asterisk = 0xf069, + icon_exclamation_sign = 0xf06a, + icon_gift = 0xf06b, + icon_leaf = 0xf06c, + icon_fire = 0xf06d, + icon_eye_open = 0xf06e, + + icon_eye_close = 0xf070, + icon_warning_sign = 0xf071, + icon_plane = 0xf072, + icon_calendar = 0xf073, + icon_random = 0xf074, + icon_comment = 0xf075, + icon_magnet = 0xf076, + icon_chevron_up = 0xf077, + icon_chevron_down = 0xf078, + icon_retweet = 0xf079, + icon_shopping_cart = 0xf07a, + icon_folder_close = 0xf07b, + icon_folder_open = 0xf07c, + icon_resize_vertical = 0xf07d, + icon_resize_horizontal = 0xf07e, + + icon_bar_chart = 0xf080, + icon_twitter_sign = 0xf081, + icon_facebook_sign = 0xf082, + icon_camera_retro = 0xf083, + icon_key = 0xf084, + icon_cogs = 0xf085, + icon_comments = 0xf086, + icon_thumbs_up = 0xf087, + icon_thumbs_down = 0xf088, + icon_star_half = 0xf089, + icon_heart_empty = 0xf08a, + icon_signout = 0xf08b, + icon_linkedin_sign = 0xf08c, + icon_pushpin = 0xf08d, + icon_external_link = 0xf08e, + + icon_signin = 0xf090, + icon_trophy = 0xf091, + icon_github_sign = 0xf092, + icon_upload_alt = 0xf093, + icon_lemon = 0xf094, + icon_phone = 0xf095, + icon_check_empty = 0xf096, + icon_bookmark_empty = 0xf097, + icon_phone_sign = 0xf098, + icon_twitter = 0xf099, + icon_facebook = 0xf09a, + icon_github = 0xf09b, + icon_unlock = 0xf09c, + icon_credit_card = 0xf09d, + icon_rss = 0xf09e, + + icon_hdd = 0xf0a0, + icon_bullhorn = 0xf0a1, + icon_bell = 0xf0a2, + icon_certificate = 0xf0a3, + icon_hand_right = 0xf0a4, + icon_hand_left = 0xf0a5, + icon_hand_up = 0xf0a6, + icon_hand_down = 0xf0a7, + icon_circle_arrow_left = 0xf0a8, + icon_circle_arrow_right = 0xf0a9, + icon_circle_arrow_up = 0xf0aa, + icon_circle_arrow_down = 0xf0ab, + icon_globe = 0xf0ac, + icon_wrench = 0xf0ad, + icon_tasks = 0xf0ae, + + icon_filter = 0xf0b0, + icon_briefcase = 0xf0b1, + icon_fullscreen = 0xf0b2, + + icon_group = 0xf0c0, + icon_link = 0xf0c1, + icon_cloud = 0xf0c2, + icon_beaker = 0xf0c3, + icon_cut = 0xf0c4, + icon_copy = 0xf0c5, + icon_paper_clip = 0xf0c6, + icon_save = 0xf0c7, + icon_sign_blank = 0xf0c8, + icon_reorder = 0xf0c9, + icon_list_ul = 0xf0ca, + icon_list_ol = 0xf0cb, + icon_strikethrough = 0xf0cc, + icon_underline = 0xf0cd, + icon_table = 0xf0ce, + + icon_magic = 0xf0d0, + icon_truck = 0xf0d1, + icon_pinterest = 0xf0d2, + icon_pinterest_sign = 0xf0d3, + icon_google_plus_sign = 0xf0d4, + icon_google_plus = 0xf0d5, + icon_money = 0xf0d6, + icon_caret_down = 0xf0d7, + icon_caret_up = 0xf0d8, + icon_caret_left = 0xf0d9, + icon_caret_right = 0xf0da, + icon_columns = 0xf0db, + icon_sort = 0xf0dc, + icon_sort_down = 0xf0dd, + icon_sort_up = 0xf0de, + + icon_envelope_alt = 0xf0e0, + icon_linkedin = 0xf0e1, + icon_undo = 0xf0e2, + icon_legal = 0xf0e3, + icon_dashboard = 0xf0e4, + icon_comment_alt = 0xf0e5, + icon_comments_alt = 0xf0e6, + icon_bolt = 0xf0e7, + icon_sitemap = 0xf0e8, + icon_umbrella = 0xf0e9, + icon_paste = 0xf0ea, + icon_lightbulb = 0xf0eb, + icon_exchange = 0xf0ec, + icon_cloud_download = 0xf0ed, + icon_cloud_upload = 0xf0ee, + + icon_user_md = 0xf0f0, + icon_stethoscope = 0xf0f1, + icon_suitcase = 0xf0f2, + icon_bell_alt = 0xf0f3, + icon_coffee = 0xf0f4, + icon_food = 0xf0f5, + icon_file_alt = 0xf0f6, + icon_building = 0xf0f7, + icon_hospital = 0xf0f8, + icon_ambulance = 0xf0f9, + icon_medkit = 0xf0fa, + icon_fighter_jet = 0xf0fb, + icon_beer = 0xf0fc, + icon_h_sign = 0xf0fd, + icon_plus_sign_alt = 0xf0fe, + + icon_double_angle_left = 0xf100, + icon_double_angle_right = 0xf101, + icon_double_angle_up = 0xf102, + icon_double_angle_down = 0xf103, + icon_angle_left = 0xf104, + icon_angle_right = 0xf105, + icon_angle_up = 0xf106, + icon_angle_down = 0xf107, + icon_desktop = 0xf108, + icon_laptop = 0xf109, + icon_tablet = 0xf10a, + icon_mobile_phone = 0xf10b, + icon_circle_blank = 0xf10c, + icon_quote_left = 0xf10d, + icon_quote_right = 0xf10e, + + icon_spinner = 0xf110, + icon_circle = 0xf111, + icon_reply = 0xf112, + icon_github_alt = 0xf113, + icon_folder_close_alt = 0xf114, + icon_folder_open_alt = 0xf115 +}; + + +//--------------------------------------------------------------------------------------- + +class QtAwesomeIconPainter; + +/// The main objet for creating icons. +/// This class requires a 2-phase construction +class QtAwesome : public QObject +{ +Q_OBJECT + +public: + + QtAwesome(QObject *parent ); + virtual ~QtAwesome(); + + void init( const QString& fontname ); + bool initFontAwesome(); + + void addNamedCodepoint( const QString& name, int codePoint ); + QHash namedCodePoints() { return namedCodepoints_; } + + void setDefaultOption( const QString& name, const QVariant& value ); + QVariant defaultOption( const QString& name ); + + QIcon icon( int character, const QVariantMap& options = QVariantMap() ); + QIcon icon( const QString& name, const QVariantMap& options = QVariantMap() ); + + void give( const QString& name, QtAwesomeIconPainter* painter ); + + /// Returns the font-name that is used as icon-map + QString fontName() { return fontName_ ; } + +private: + QString fontName_; ///< The font name used for this map + QHash namedCodepoints_; ///< A map with names mapped to code-points + + QHash painterMap_; ///< A map of custom painters + QVariantMap defaultOptions_; ///< The default icon options + QtAwesomeIconPainter* fontIconPainter_; ///< A special painter fo painting codepoints +}; + + +//--------------------------------------------------------------------------------------- + + +/// The QtAwesomeIconPainter is a specialized painter for painting icons +/// your can implement an iconpainter to create custom font-icon code +class QtAwesomeIconPainter +{ +public: + virtual ~QtAwesomeIconPainter() {} + virtual void paint( QtAwesome* awesome, QPainter* painter, const QRect& rect, QIcon::Mode mode, QIcon::State state, const QVariantMap& options ) = 0; +}; + + + +#endif // QTAWESOME_H diff --git a/QtAwesome/QtAwesome.pri b/QtAwesome/QtAwesome.pri new file mode 100644 index 0000000..5e4002d --- /dev/null +++ b/QtAwesome/QtAwesome.pri @@ -0,0 +1,10 @@ + +INCLUDEPATH += $$PWD + +SOURCES += $$PWD/QtAwesome.cpp + +HEADERS += $$PWD/QtAwesome.h + +RESOURCES += $$PWD/QtAwesome.qrc + + diff --git a/QtAwesome/QtAwesome.pro b/QtAwesome/QtAwesome.pro new file mode 100644 index 0000000..e6c1ece --- /dev/null +++ b/QtAwesome/QtAwesome.pro @@ -0,0 +1,15 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2013-04-18T13:28:42 +# +#------------------------------------------------- + +TARGET = QtAwesome +TEMPLATE = lib +CONFIG += staticlib + +SOURCES += QtAwesome.cpp +HEADERS += QtAwesome.h + +RESOURCES += \ + QtAwesome.qrc diff --git a/QtAwesome/QtAwesome.qrc b/QtAwesome/QtAwesome.qrc new file mode 100644 index 0000000..40ffd6a --- /dev/null +++ b/QtAwesome/QtAwesome.qrc @@ -0,0 +1,5 @@ + + + fonts/fontawesome.ttf + + diff --git a/QtAwesome/fonts/fontawesome.ttf b/QtAwesome/fonts/fontawesome.ttf new file mode 100755 index 0000000..d461724 Binary files /dev/null and b/QtAwesome/fonts/fontawesome.ttf differ diff --git a/QtAwesomeSample/QtAwesomeSample.pro b/QtAwesomeSample/QtAwesomeSample.pro new file mode 100644 index 0000000..3f9082a --- /dev/null +++ b/QtAwesomeSample/QtAwesomeSample.pro @@ -0,0 +1,20 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2013-04-18T15:05:07 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = QtAwesomeSample +TEMPLATE = app + + +SOURCES += main.cpp + +HEADERS += + + +include(../QtAwesome/QtAwesome.pri) diff --git a/QtAwesomeSample/main.cpp b/QtAwesomeSample/main.cpp new file mode 100644 index 0000000..146439f --- /dev/null +++ b/QtAwesomeSample/main.cpp @@ -0,0 +1,26 @@ +/** + * Copyright 2011-2013 - Reliable Bits Software by Blommers IT. All Rights Reserved. + * Author Rick Blommers + */ + +#include "QtAwesome.h" + +#include +#include +#include + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + QMainWindow w; + + QtAwesome* awesome = new QtAwesome(&w); + awesome->initFontAwesome(); + + // a simple beer button + QPushButton* beerButton = new QPushButton( awesome->icon( icon_beer), "Cheers!" ); + w.setCentralWidget( beerButton ); + w.show(); + + return app.exec(); +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..e5c5d2a --- /dev/null +++ b/README.md @@ -0,0 +1,117 @@ +QtAwesome - FontAwesome support for Qt applications +=================================================== + +Description +----------- + +QtAwesome is a simple library that can be used to add font-awesome icons to your Qt application. +Though the name is QtAwesome and currently it's very FontAwesome based, you can use every other +iconfont you want. + +The class can also be used to manage your own dynamic code-drawn icons, by adding named icon-painters. + + +Installation +------------ + +The easiest way to include QtAweome in your project is to copy the QtAwesome directory to your +project tree and add the the following code include to your Qt project file: + + include(QtAwesome/QtAwesome.pri) + +Now your good to go!! + + +Usage +----- + +You probably want to create a single QtAwesome object for your whole application. + + QtAwesome* awesome = new QtAwesome( qApp ) + awesome->initFontAwesome(); // This line is important as it loads the font and initializes the named icon map + +Add an accessor to this object. (a global function, membor of your application object whatever you like). + + +Example +-------- + + // You should create a single object of QtAwesome. + QtAwesome* awesome = new QtAwesome( qApp ); + awesome->initFontAwesome(); + + // Next create your icon with the help of the icon-enumeration: (all dashes are replaced by underscores) + QPushButton* beerButton new QPushButton( awesome->icon( icon_beer ), "Cheers!" ); + + // You can also use 'string' names to access the icons. (The string version omits the 'icon-' prefix ) + QPushButton* coffeeButton new QPushButton( awesome->icon( "coffee" ), "Black please!" ); + + // When you create an icon you can supply some options for your icons: + // The available options can be found at the "Default options"-section + + QVariantMap options; + options.insert( "color" , QColor(255,0,0) ); + QPushButton* musicButton = new QPushButton( awesome->icon( icon_music ), "Music" ); + + // You can also change the default options. + // for example if you always would like to have green icons you could call) + awesome->setDefaultOption( "color-disabled", QColor(0,255,0) ); + + + +Default options: +---------------- + + The following options are default in the QtAwesome class. + + setDefaultOption( "color", QColor(50,50,50) ); + setDefaultOption( "color-disabled", QColor(70,70,70,60)); + setDefaultOption( "color-active", QColor(10,10,10)); + setDefaultOption( "color-selected", QColor(10,10,10)); + setDefaultOption( "scale-factor", 0.9 ); + + When creating an icon, it first populates the options-map with the default options from the QtAwesome object. + After that the options are expanded/overwritten by the options supplied to the icon. + + +License +------- + +MIT License. Copyright 2013 - Reliable Bits Software by Blommers IT. [http://blommersit.nl/](http://blommersit.nl) + +The Font Awesome font is licensed under the SIL Open Font License - [http://scripts.sil.org/OFL](http://scripts.sil.org/OFL) +The Font Awesome pictograms are licensed under the CC BY 3.0 License - [http://creativecommons.org/licenses/by/3.0/](http://creativecommons.org/licenses/by/3.0/) +"Font Awesome by Dave Gandy - http://fortawesome.github.com/Font-Awesome" + +Contact +------- + +* email: +* twitter: [https://twitter.com/gamecreature](https://twitter.com/gamecreature) +* website: [http://blommersit.nl](http://blommersit.nl) (warning Dutch content ahead) +* github: [https://github.com/gamecreature/QtAwesome](https://github.com/gamecreature/QtAwesome) + +Remarks +------- + +I've created this project because I needed some nice icons for my own Qt project. After doing a lot of +css/html5 work and being spoiled by the ease of twitter bootstrap with FontAwesome, +I thought it would be nice to be able to use these icons for my Qt project. + +I've slightly changed the code from the original, added some more documentation, but it's still +a work in progress. So feel free to drop me an e-mail for your suggestions and improvements! + +There are still some things todo, like: + + * document the usage of a custom icon painter + * document the usage of another icon font + * add some tests + * do some code cleanup + +And of course last but not least, + +Many thanks go to Dave Gandy an the other FontAwesome contributors!! [http://fortawesome.github.com/Font-Awesome](http://fortawesome.github.com/Font-Awesome) +And of course to the Qt team/contributors for supplying this great cross-platform c++ library. + + +Contributions are welcome! Feel free to fork and send a pull request through Github.