_uiStylePalette = QVector<QBrush>(UiStyle::NumRoles, QBrush());
_uiStyleColorRoles["marker-line"] = UiStyle::MarkerLine;
- _uiStyleColorRoles["active-nick"] = UiStyle::ActiveNick;
- _uiStyleColorRoles["inactive-nick"] = UiStyle::InactiveNick;
- _uiStyleColorRoles["channel"] = UiStyle::Channel;
- _uiStyleColorRoles["inactive-channel"] = UiStyle::InactiveChannel;
- _uiStyleColorRoles["active-channel"] = UiStyle::ActiveChannel;
- _uiStyleColorRoles["unread-channel"] = UiStyle::UnreadChannel;
- _uiStyleColorRoles["highlighted-channel"] = UiStyle::HighlightedChannel;
- _uiStyleColorRoles["query"] = UiStyle::Query;
- _uiStyleColorRoles["inactive-query"] = UiStyle::InactiveQuery;
- _uiStyleColorRoles["active-query"] = UiStyle::ActiveQuery;
- _uiStyleColorRoles["unread-query"] = UiStyle::UnreadQuery;
- _uiStyleColorRoles["highlighted-query"] = UiStyle::HighlightedQuery;
}
void QssParser::processStyleSheet(QString &ss) {
QRegExp paletterx("(Palette[^{]*)\\{([^}]+)\\}");
int pos = 0;
while((pos = paletterx.indexIn(ss, pos)) >= 0) {
- parsePaletteData(paletterx.cap(1).trimmed(), paletterx.cap(2).trimmed());
+ parsePaletteBlock(paletterx.cap(1).trimmed(), paletterx.cap(2).trimmed());
ss.remove(pos, paletterx.matchedLength());
}
// Now we can parse the rest of our custom blocks
- QRegExp blockrx("((?:ChatLine|BufferList|NickList|TreeView)[^{]*)\\{([^}]+)\\}");
+ QRegExp blockrx("((?:ChatLine|ChatListItem|NickListItem)[^{]*)\\{([^}]+)\\}");
pos = 0;
while((pos = blockrx.indexIn(ss, pos)) >= 0) {
//qDebug() << blockrx.cap(1) << blockrx.cap(2);
+ QString declaration = blockrx.cap(1).trimmed();
+ QString contents = blockrx.cap(2).trimmed();
- if(blockrx.cap(1).startsWith("ChatLine"))
- parseChatLineData(blockrx.cap(1).trimmed(), blockrx.cap(2).trimmed());
+ if(declaration.startsWith("ChatLine"))
+ parseChatLineBlock(declaration, contents);
+ else if(declaration.startsWith("ChatListItem") || declaration.startsWith("NickListItem"))
+ parseListItemBlock(declaration, contents);
//else
// TODO: add moar here
}
}
-void QssParser::parseChatLineData(const QString &decl, const QString &contents) {
+/******** Parse a whole block: declaration { contents } *******/
+
+void QssParser::parseChatLineBlock(const QString &decl, const QString &contents) {
quint64 fmtType = parseFormatType(decl);
if(fmtType == UiStyle::Invalid)
return;
- QTextCharFormat format;
+ _formats[fmtType].merge(parseFormat(contents));
+}
+
+void QssParser::parseListItemBlock(const QString &decl, const QString &contents) {
+ quint32 fmtType = parseItemFormatType(decl);
+ if(fmtType == UiStyle::Invalid)
+ return;
+
+ _listItemFormats[fmtType].merge(parseFormat(contents));
+}
+
+// Palette { ... } specifies the application palette
+// ColorGroups can be specified like pseudo states, chaining is OR (contrary to normal CSS handling):
+// Palette:inactive:disabled { ... } applies to both the Inactive and the Disabled state
+void QssParser::parsePaletteBlock(const QString &decl, const QString &contents) {
+ QList<QPalette::ColorGroup> colorGroups;
+
+ // Check if we want to apply this palette definition for particular ColorGroups
+ QRegExp rx("Palette((:(normal|active|inactive|disabled))*)");
+ if(!rx.exactMatch(decl)) {
+ qWarning() << Q_FUNC_INFO << tr("Invalid block declaration: %1").arg(decl);
+ return;
+ }
+ if(!rx.cap(1).isEmpty()) {
+ QStringList groups = rx.cap(1).split(':', QString::SkipEmptyParts);
+ foreach(QString g, groups) {
+ if((g == "normal" || g == "active") && !colorGroups.contains(QPalette::Active))
+ colorGroups.append(QPalette::Active);
+ else if(g == "inactive" && !colorGroups.contains(QPalette::Inactive))
+ colorGroups.append(QPalette::Inactive);
+ else if(g == "disabled" && !colorGroups.contains(QPalette::Disabled))
+ colorGroups.append(QPalette::Disabled);
+ }
+ }
+ // Now let's go through the roles
foreach(QString line, contents.split(';', QString::SkipEmptyParts)) {
int idx = line.indexOf(':');
if(idx <= 0) {
- qWarning() << Q_FUNC_INFO << tr("Invalid property declaration: %1").arg(line.trimmed());
+ qWarning() << Q_FUNC_INFO << tr("Invalid palette role assignment: %1").arg(line.trimmed());
continue;
}
- QString property = line.left(idx).trimmed();
- QString value = line.mid(idx + 1).simplified();
-
- if(property == "background" || property == "background-color")
- format.setBackground(parseBrush(value));
- else if(property == "foreground" || property == "color")
- format.setForeground(parseBrush(value));
-
- // font-related properties
- else if(property.startsWith("font")) {
- if(property == "font")
- parseFont(value, &format);
- else if(property == "font-style")
- parseFontStyle(value, &format);
- else if(property == "font-weight")
- parseFontWeight(value, &format);
- else if(property == "font-size")
- parseFontSize(value, &format);
- else if(property == "font-family")
- parseFontFamily(value, &format);
- else {
- qWarning() << Q_FUNC_INFO << tr("Invalid font property: %1").arg(line);
- continue;
- }
- }
+ QString rolestr = line.left(idx).trimmed();
+ QString brushstr = line.mid(idx + 1).trimmed();
- else {
- qWarning() << Q_FUNC_INFO << tr("Unknown ChatLine property: %1").arg(property);
- }
+ if(_paletteColorRoles.contains(rolestr)) {
+ QBrush brush = parseBrush(brushstr);
+ if(colorGroups.count()) {
+ foreach(QPalette::ColorGroup group, colorGroups)
+ _palette.setBrush(group, _paletteColorRoles.value(rolestr), brush);
+ } else
+ _palette.setBrush(_paletteColorRoles.value(rolestr), brush);
+ } else if(_uiStyleColorRoles.contains(rolestr)) {
+ _uiStylePalette[_uiStyleColorRoles.value(rolestr)] = parseBrush(brushstr);
+ } else
+ qWarning() << Q_FUNC_INFO << tr("Unknown palette role name: %1").arg(rolestr);
}
-
- _formats[fmtType].merge(format);
}
+/******** Determine format types from a block declaration ********/
+
quint64 QssParser::parseFormatType(const QString &decl) {
- QRegExp rx("ChatLine(?:::(\\w+))?(?:#(\\w+))?(?:\\[([=-,\\\"\\w\\s]+)\\])?\\s*");
+ QRegExp rx("ChatLine(?:::(\\w+))?(?:#(\\w+))?(?:\\[([=-,\\\"\\w\\s]+)\\])?");
// $1: subelement; $2: msgtype; $3: conditionals
if(!rx.exactMatch(decl)) {
qWarning() << Q_FUNC_INFO << tr("Invalid block declaration: %1").arg(decl);
return fmtType;
}
-// Palette { ... } specifies the application palette
-// ColorGroups can be specified like pseudo states, chaining is OR (contrary to normal CSS handling):
-// Palette:inactive:disabled { ... } applies to both the Inactive and the Disabled state
-void QssParser::parsePaletteData(const QString &decl, const QString &contents) {
- QList<QPalette::ColorGroup> colorGroups;
-
- // Check if we want to apply this palette definition for particular ColorGroups
- QRegExp rx("Palette((:(normal|active|inactive|disabled))*)");
+// FIXME: Code duplication
+quint32 QssParser::parseItemFormatType(const QString &decl) {
+ QRegExp rx("(Chat|Nick)ListItem(?:\\[([=-,\\\"\\w\\s]+)\\])?");
+ // $1: item type; $2: properties
if(!rx.exactMatch(decl)) {
qWarning() << Q_FUNC_INFO << tr("Invalid block declaration: %1").arg(decl);
- return;
+ return UiStyle::Invalid;
}
- if(!rx.cap(1).isEmpty()) {
- QStringList groups = rx.cap(1).split(':', QString::SkipEmptyParts);
- foreach(QString g, groups) {
- if((g == "normal" || g == "active") && !colorGroups.contains(QPalette::Active))
- colorGroups.append(QPalette::Active);
- else if(g == "inactive" && !colorGroups.contains(QPalette::Inactive))
- colorGroups.append(QPalette::Inactive);
- else if(g == "disabled" && !colorGroups.contains(QPalette::Disabled))
- colorGroups.append(QPalette::Disabled);
+ QString mainItemType = rx.cap(1);
+ QString properties = rx.cap(2);
+
+ quint32 fmtType = 0;
+
+ // Next up: properties
+ QString type, state;
+ if(!properties.isEmpty()) {
+ QHash<QString, QString> props;
+ QRegExp propRx("\\s*([\\w\\-]+)\\s*=\\s*\"([\\w\\-]+)\"\\s*");
+ foreach(const QString &prop, properties.split(',', QString::SkipEmptyParts)) {
+ if(!propRx.exactMatch(prop)) {
+ qWarning() << Q_FUNC_INFO << tr("Invalid proplist %1").arg(prop);
+ return UiStyle::Invalid;
+ }
+ props[propRx.cap(1)] = propRx.cap(2);
}
+ type = props.value("type");
+ state = props.value("state");
}
- // Now let's go through the roles
- foreach(QString line, contents.split(';', QString::SkipEmptyParts)) {
+ if(mainItemType == "Chat") {
+ fmtType |= UiStyle::BufferViewItem;
+ if(!type.isEmpty()) {
+ if(type == "network")
+ fmtType |= UiStyle::NetworkItem;
+ else if(type == "channel")
+ fmtType |= UiStyle::ChannelBufferItem;
+ else if(type == "query")
+ fmtType |= UiStyle::QueryBufferItem;
+ else {
+ qWarning() << Q_FUNC_INFO << tr("Invalid chatlist item type %1").arg(type);
+ return UiStyle::Invalid;
+ }
+ }
+ if(!state.isEmpty()) {
+ if(state == "inactive")
+ fmtType |= UiStyle::InactiveBuffer;
+ else if(state == "event")
+ fmtType |= UiStyle::ActiveBuffer;
+ else if(state == "unread-message")
+ fmtType |= UiStyle::UnreadBuffer;
+ else if(state == "highlighted")
+ fmtType |= UiStyle::HighlightedBuffer;
+ else if(state == "away")
+ fmtType |= UiStyle::UserAway;
+ else {
+ qWarning() << Q_FUNC_INFO << tr("Invalid chatlist state %1").arg(state);
+ return UiStyle::Invalid;
+ }
+ }
+ } else { // NickList
+ fmtType |= UiStyle::NickViewItem;
+ if(!type.isEmpty()) {
+ if(type == "user") {
+ fmtType |= UiStyle::IrcUserItem;
+ if(state == "away")
+ fmtType |= UiStyle::UserAway;
+ } else if(type == "category")
+ fmtType |= UiStyle::UserCategoryItem;
+ }
+ }
+ return fmtType;
+}
+
+/******** Parse a whole format attribute block ********/
+
+QTextCharFormat QssParser::parseFormat(const QString &qss) {
+ QTextCharFormat format;
+
+ foreach(QString line, qss.split(';', QString::SkipEmptyParts)) {
int idx = line.indexOf(':');
if(idx <= 0) {
- qWarning() << Q_FUNC_INFO << tr("Invalid palette role assignment: %1").arg(line.trimmed());
+ qWarning() << Q_FUNC_INFO << tr("Invalid property declaration: %1").arg(line.trimmed());
continue;
}
- QString rolestr = line.left(idx).trimmed();
- QString brushstr = line.mid(idx + 1).trimmed();
+ QString property = line.left(idx).trimmed();
+ QString value = line.mid(idx + 1).simplified();
- if(_paletteColorRoles.contains(rolestr)) {
- QBrush brush = parseBrush(brushstr);
- if(colorGroups.count()) {
- foreach(QPalette::ColorGroup group, colorGroups)
- _palette.setBrush(group, _paletteColorRoles.value(rolestr), brush);
- } else
- _palette.setBrush(_paletteColorRoles.value(rolestr), brush);
- } else if(_uiStyleColorRoles.contains(rolestr)) {
- _uiStylePalette[_uiStyleColorRoles.value(rolestr)] = parseBrush(brushstr);
- } else
- qWarning() << Q_FUNC_INFO << tr("Unknown palette role name: %1").arg(rolestr);
+ if(property == "background" || property == "background-color")
+ format.setBackground(parseBrush(value));
+ else if(property == "foreground" || property == "color")
+ format.setForeground(parseBrush(value));
+
+ // font-related properties
+ else if(property.startsWith("font")) {
+ if(property == "font")
+ parseFont(value, &format);
+ else if(property == "font-style")
+ parseFontStyle(value, &format);
+ else if(property == "font-weight")
+ parseFontWeight(value, &format);
+ else if(property == "font-size")
+ parseFontSize(value, &format);
+ else if(property == "font-family")
+ parseFontFamily(value, &format);
+ else {
+ qWarning() << Q_FUNC_INFO << tr("Invalid font property: %1").arg(line);
+ continue;
+ }
+ }
+
+ else {
+ qWarning() << Q_FUNC_INFO << tr("Unknown ChatLine property: %1").arg(property);
+ }
}
+
+ return format;
}
+/******** Brush ********/
+
QBrush QssParser::parseBrush(const QString &str, bool *ok) {
if(ok)
*ok = false;
***************************************************************************/
#include <QApplication>
+#include "buffersettings.h"
+#include "iconloader.h"
#include "qssparser.h"
#include "quassel.h"
#include "uistyle.h"
QHash<QString, UiStyle::FormatType> UiStyle::_formatCodes;
QString UiStyle::_timestampFormatString;
-UiStyle::UiStyle(QObject *parent) : QObject(parent) {
+UiStyle::UiStyle(QObject *parent)
+: QObject(parent),
+ _channelJoinedIcon(SmallIcon("irc-channel-active")),
+ _channelPartedIcon(SmallIcon("irc-channel-inactive")),
+ _userOfflineIcon(SmallIcon("im-user-offline")),
+ _userOnlineIcon(SmallIcon("im-user")),
+ _userAwayIcon(SmallIcon("im-user-away")),
+ _categoryOpIcon(SmallIcon("irc-operator")),
+ _categoryVoiceIcon(SmallIcon("irc-voice")),
+ _opIconLimit(UserCategoryItem::categoryFromModes("o")),
+ _voiceIconLimit(UserCategoryItem::categoryFromModes("v"))
+{
// register FormatList if that hasn't happened yet
// FIXME I don't think this actually avoids double registration... then again... does it hurt?
if(QVariant::nameToType("UiStyle::FormatList") == QVariant::Invalid) {
setTimestampFormatString("[hh:mm:ss]");
+ // BufferView / NickView settings
+ BufferSettings bufferSettings;
+ _showBufferViewIcons = _showNickViewIcons = bufferSettings.showUserStateIcons();
+ bufferSettings.notify("ShowUserStateIcons", this, SLOT(showUserStateIconsChanged()));
+
loadStyleSheet();
}
fmt.merge(parser.formats().value(fmtType));
_formatCache[fmtType] = fmt;
}
+ _listItemFormats = parser.listItemFormats();
qApp->setStyleSheet(styleSheet); // pass the remaining sections to the application
}
}
}
+/******** ItemView Styling *******/
+
+void UiStyle::showUserStateIconsChanged() {
+ BufferSettings bufferSettings;
+ _showBufferViewIcons = _showNickViewIcons = bufferSettings.showUserStateIcons();
+}
+
+QVariant UiStyle::bufferViewItemData(const QModelIndex &index, int role) const {
+ BufferInfo::Type type = (BufferInfo::Type)index.data(NetworkModel::BufferTypeRole).toInt();
+ bool isActive = index.data(NetworkModel::ItemActiveRole).toBool();
+
+ if(role == Qt::DecorationRole) {
+ if(!_showBufferViewIcons)
+ return QVariant();
+
+ switch(type) {
+ case BufferInfo::ChannelBuffer:
+ if(isActive)
+ return _channelJoinedIcon;
+ else
+ return _channelPartedIcon;
+ case BufferInfo::QueryBuffer:
+ if(!isActive)
+ return _userOfflineIcon;
+ if(index.data(NetworkModel::UserAwayRole).toBool())
+ return _userAwayIcon;
+ else
+ return _userOnlineIcon;
+ default:
+ return QVariant();
+ }
+ }
+
+ quint32 fmtType = BufferViewItem;
+ switch(type) {
+ case BufferInfo::StatusBuffer:
+ fmtType |= NetworkItem;
+ break;
+ case BufferInfo::ChannelBuffer:
+ fmtType |= ChannelBufferItem;
+ break;
+ case BufferInfo::QueryBuffer:
+ fmtType |= QueryBufferItem;
+ break;
+ default:
+ return QVariant();
+ }
+
+ QTextCharFormat fmt = _listItemFormats.value(BufferViewItem);
+ fmt.merge(_listItemFormats.value(fmtType));
+
+ BufferInfo::ActivityLevel activity = (BufferInfo::ActivityLevel)index.data(NetworkModel::BufferActivityRole).toInt();
+ if(activity & BufferInfo::Highlight) {
+ fmt.merge(_listItemFormats.value(BufferViewItem | HighlightedBuffer));
+ fmt.merge(_listItemFormats.value(fmtType | HighlightedBuffer));
+ } else if(activity & BufferInfo::NewMessage) {
+ fmt.merge(_listItemFormats.value(BufferViewItem | UnreadBuffer));
+ fmt.merge(_listItemFormats.value(fmtType | UnreadBuffer));
+ } else if(activity & BufferInfo::OtherActivity) {
+ fmt.merge(_listItemFormats.value(BufferViewItem | ActiveBuffer));
+ fmt.merge(_listItemFormats.value(fmtType | ActiveBuffer));
+ } else if(!isActive) {
+ fmt.merge(_listItemFormats.value(BufferViewItem | InactiveBuffer));
+ fmt.merge(_listItemFormats.value(fmtType | InactiveBuffer));
+ } else if(index.data(NetworkModel::UserAwayRole).toBool()) {
+ fmt.merge(_listItemFormats.value(BufferViewItem | UserAway));
+ fmt.merge(_listItemFormats.value(fmtType | UserAway));
+ }
+
+ return itemData(role, fmt);
+}
+
+QVariant UiStyle::nickViewItemData(const QModelIndex &index, int role) const {
+ NetworkModel::ItemType type = (NetworkModel::ItemType)index.data(NetworkModel::ItemTypeRole).toInt();
+
+ if(role == Qt::DecorationRole) {
+ if(!_showNickViewIcons)
+ return QVariant();
+
+ switch(type) {
+ case NetworkModel::UserCategoryItemType:
+ {
+ int categoryId = index.data(TreeModel::SortRole).toInt();
+ if(categoryId <= _opIconLimit)
+ return _categoryOpIcon;
+ if(categoryId <= _voiceIconLimit)
+ return _categoryVoiceIcon;
+ return _userOnlineIcon;
+ }
+ case NetworkModel::IrcUserItemType:
+ if(index.data(NetworkModel::ItemActiveRole).toBool())
+ return _userOnlineIcon;
+ else
+ return _userAwayIcon;
+ default:
+ return QVariant();
+ }
+ }
+
+ QTextCharFormat fmt = _listItemFormats.value(NickViewItem);
+
+ switch(type) {
+ case NetworkModel::IrcUserItemType:
+ fmt.merge(_listItemFormats.value(NickViewItem | IrcUserItem));
+ if(!index.data(NetworkModel::ItemActiveRole).toBool()) {
+ fmt.merge(_listItemFormats.value(NickViewItem | UserAway));
+ fmt.merge(_listItemFormats.value(NickViewItem | IrcUserItem | UserAway));
+ }
+ break;
+ case NetworkModel::UserCategoryItemType:
+ fmt.merge(_listItemFormats.value(NickViewItem | UserCategoryItem));
+ break;
+ default:
+ return QVariant();
+ }
+
+ return itemData(role, fmt);
+}
+
+QVariant UiStyle::itemData(int role, const QTextCharFormat &format) const {
+ switch(role) {
+ case Qt::FontRole:
+ return format.font();
+ case Qt::ForegroundRole:
+ return format.property(QTextFormat::ForegroundBrush);
+ case Qt::BackgroundRole:
+ return format.property(QTextFormat::BackgroundBrush);
+ default:
+ return QVariant();
+ }
+}
+
/******** Caching *******/
QTextCharFormat UiStyle::cachedFormat(quint64 key) const {
#include <QPalette>
#include <QVector>
+#include "bufferinfo.h"
#include "message.h"
+#include "networkmodel.h"
#include "settings.h"
class UiStyle : public QObject{
Selected = 0x00000004 // must be last!
};
+ enum ItemFormatType {
+ BufferViewItem = 0x00000001,
+ NickViewItem = 0x00000002,
+
+ NetworkItem = 0x00000010,
+ ChannelBufferItem = 0x00000020,
+ QueryBufferItem = 0x00000040,
+ IrcUserItem = 0x00000080,
+ UserCategoryItem = 0x00000100,
+
+ InactiveBuffer = 0x00001000,
+ ActiveBuffer = 0x00002000,
+ UnreadBuffer = 0x00004000,
+ HighlightedBuffer = 0x00008000,
+ UserAway = 0x00010000
+ };
+
enum ColorRole {
MarkerLine,
- ActiveNick,
- InactiveNick,
- Channel,
- InactiveChannel,
- ActiveChannel,
- UnreadChannel,
- HighlightedChannel,
- Query,
- InactiveQuery,
- ActiveQuery,
- UnreadQuery,
- HighlightedQuery,
NumRoles // must be last!
};
QTextCharFormat format(quint32 formatType, quint32 messageLabel = 0);
QFontMetricsF *fontMetrics(quint32 formatType, quint32 messageLabel = 0);
- inline QFont defaultFont() const { return _defaultFont; }
+ QList<QTextLayout::FormatRange> toTextLayoutList(const FormatList &, int textLength, quint32 messageLabel = 0);
inline const QBrush &brush(ColorRole role) const { return _uiStylePalette.at((int) role); }
inline void setBrush(ColorRole role, const QBrush &brush) { _uiStylePalette[(int) role] = brush; }
- QList<QTextLayout::FormatRange> toTextLayoutList(const FormatList &, int textLength, quint32 messageLabel = 0);
+ QVariant bufferViewItemData(const QModelIndex &networkModelIndex, int role) const;
+ QVariant nickViewItemData(const QModelIndex &networkModelIndex, int role) const;
public slots:
void reload();
static QString formatCode(FormatType);
static void setTimestampFormatString(const QString &format);
+ QVariant itemData(int role, const QTextCharFormat &format) const;
+
+private slots:
+ void showUserStateIconsChanged();
+
private:
- QFont _defaultFont;
QVector<QBrush> _uiStylePalette;
QBrush _markerLineBrush;
QHash<quint64, QTextCharFormat> _formatCache;
QHash<quint64, QFontMetricsF *> _metricsCache;
+ QHash<quint32, QTextCharFormat> _listItemFormats;
static QHash<QString, FormatType> _formatCodes;
static QString _timestampFormatString;
+
+ QPixmap _channelJoinedIcon;
+ QPixmap _channelPartedIcon;
+ QPixmap _userOfflineIcon;
+ QPixmap _userOnlineIcon;
+ QPixmap _userAwayIcon;
+ QPixmap _categoryOpIcon;
+ QPixmap _categoryVoiceIcon;
+ int _opIconLimit;
+ int _voiceIconLimit;
+ bool _showNickViewIcons;
+ bool _showBufferViewIcons;
};
class UiStyle::StyledMessage : public Message {