provided as a role in ChatlineModel.
Now I just need to implement the whole view thing... well, actually
displaying messages is probably totally overrated anyway...
qRegisterMetaTypeStreamOperators<UserId>("UserId");
qRegisterMetaTypeStreamOperators<AccountId>("AccountId");
qRegisterMetaTypeStreamOperators<MsgId>("MsgId");
-
}
// Static variables
_flags = flags;
}
+#ifndef SPUTDEV
QString Message::mircToInternal(QString mirc) {
mirc.replace('%', "%%"); // escape % just to be sure
mirc.replace('\x02', "%B");
format();
return _formattedText;
}
-
-/*
-QString Message::formattedToHtml(const QString &f) {
-
-
- return f;
-}
-*/
+#endif /* SPUTDEV */
QDataStream &operator<<(QDataStream &out, const Message &msg) {
out << msg.msgId() << (quint32)msg.timestamp().toTime_t() << (quint32)msg.type() << (quint8)msg.flags()
void setFlags(Flags flags);
+#ifndef SPUTDEV
QString formattedTimestamp();
QString formattedSender();
QString formattedText();
static QString mircToInternal(QString);
void format();
+#endif
private:
QDateTime _timestamp;
inline QPersistentModelIndex persistentIndex() const { return _index; }
inline const MessageModel *model() const { return _index.isValid() ? qobject_cast<const MessageModel *>(_index.model()) : 0; }
inline int row() const { return _index.isValid() ? _index.row() : 0; }
- QVariant data(int role) const;
+ virtual QVariant data(int role) const;
//QString text() const;
//void setText(const UiStyle::StyledText &text);
***************************************************************************/
#include "chatline.h"
+#include "chatlinemodel.h"
+#include "qtui.h"
+#include "uistyle.h"
Chatline::Chatline(const Message &msg) : MessageItem(msg) {
- _msg = msg;
-
+ _msg = QtUi::style()->styleMessage(msg);
}
QVariant Chatline::data(int column, int role) const {
switch(role) {
- case MessageModel::DisplayRole: return _msg.text();
- default: return MessageItem::data(column, role);
+ case ChatlineModel::DisplayRole:
+ switch(column) {
+ case ChatlineModel::TimestampColumn: return _msg.timestamp.text;
+ case ChatlineModel::SenderColumn: return _msg.sender.text;
+ case ChatlineModel::TextColumn: return _msg.text.text;
+ }
+ break;
+ case ChatlineModel::FormatRole:
+ switch(column) {
+ case ChatlineModel::TimestampColumn: return QVariant::fromValue<UiStyle::FormatList>(_msg.timestamp.formats);
+ case ChatlineModel::SenderColumn: return QVariant::fromValue<UiStyle::FormatList>(_msg.sender.formats);
+ case ChatlineModel::TextColumn: return QVariant::fromValue<UiStyle::FormatList>(_msg.text.formats);
+ }
+ break;
}
+ return MessageItem::data(column, role);
}
bool Chatline::setData(int column, const QVariant &value, int role) {
#define CHATLINE_H_
#include "messagemodel.h"
+#include "uistyle.h"
class Chatline : public MessageItem {
virtual bool setData(int column, const QVariant &value, int role);
private:
- Message _msg;
+ UiStyle::StyledMessage _msg;
};
#endif
Q_OBJECT
public:
+ enum ChatlineRole {
+ FormatRole = MessageModel::UserRole
+ };
+
ChatlineModel(QObject *parent = 0);
virtual ~ChatlineModel();
UiSettings uiSettings;
+#ifndef SPUTDEV
if(uiSettings.value("DisplayPopupMessages", QVariant(true)).toBool()) {
// FIXME don't invoke style engine for this!
QString text = QtUi::style()->styleString(Message::mircToInternal(msg.text())).text;
displayTrayIconMessage(title, text);
}
-
+#endif
if(uiSettings.value("AnimateTrayIcon", QVariant(true)).toBool()) {
QApplication::alert(this);
setTrayIconActivity(true);
QPainter painter(this);
painter.setBackgroundMode(Qt::OpaqueMode);
+ // FIXME re-enable topic painting
+#ifndef SPUTDEV
QRect drawRect = rect();
QRect brect;
QString textPart;
painter.drawText(drawRect, Qt::AlignLeft|Qt::TextSingleLine, textPart, &brect);
drawRect.setLeft(brect.right());
}
+#endif
}
void TopicButton::setAndStyleText(const QString &text) {
setText(text); // this triggers a repaint event
+#ifndef SPUTDEV
styledText = QtUi::style()->styleString(Message::mircToInternal(text));
int height = 1;
foreach(QTextLayout::FormatRange fr, styledText.formats) {
height = QFontMetrics(qApp->font()).height();
setFixedHeight(height);
-
+#endif
// show topic in tooltip
setToolTip(tr("%1\n\nClick to edit!").arg(QAbstractButton::text()));
}
virtual void paintEvent(QPaintEvent *event);
private:
+#ifndef SPUTDEV
UiStyle::StyledText styledText;
+#endif
QSize _sizeHint;
};
#include "uistyle.h"
#include "uistylesettings.h"
+#include "util.h"
UiStyle::UiStyle(const QString &settingsKey) : _settingsKey(settingsKey) {
+ // register FormatList if that hasn't happened yet
+ // FIXME I don't think this actually avoids double registration... :/
+ if(QVariant::nameToType("UiStyle::FormatList") == QVariant::Invalid) {
+ qRegisterMetaType<FormatList>("UiStyle::FormatList");
+ qRegisterMetaTypeStreamOperators<FormatList>("UiStyle::FormatList");
+ Q_ASSERT(QVariant::nameToType("UiStyle::FormatList") != QVariant::Invalid);
+ }
+
// Default format
_defaultPlainFormat.setForeground(QBrush("#000000"));
_defaultPlainFormat.setFont(QFont("Monospace", QApplication::font().pointSize()));
result.text = s;
return result;
}
+
+QString UiStyle::mircToInternal(const QString &mirc_) {
+ QString mirc = mirc_;
+ mirc.replace('%', "%%"); // escape % just to be sure
+ mirc.replace('\x02', "%B");
+ mirc.replace('\x0f', "%O");
+ mirc.replace('\x12', "%R");
+ mirc.replace('\x16', "%R");
+ mirc.replace('\x1d', "%S");
+ mirc.replace('\x1f', "%U");
+
+ // Now we bring the color codes (\x03) in a sane format that can be parsed more easily later.
+ // %Dcfxx is foreground, %Dcbxx is background color, where xx is a 2 digit dec number denoting the color code.
+ // %Dc- turns color off.
+ // Note: We use the "mirc standard" as described in <http://www.mirc.co.uk/help/color.txt>.
+ // This means that we don't accept something like \x03,5 (even though others, like WeeChat, do).
+ int pos = 0;
+ for(;;) {
+ pos = mirc.indexOf('\x03', pos);
+ if(pos < 0) break; // no more mirc color codes
+ QString ins, num;
+ int l = mirc.length();
+ int i = pos + 1;
+ // check for fg color
+ if(i < l && mirc[i].isDigit()) {
+ num = mirc[i++];
+ if(i < l && mirc[i].isDigit()) num.append(mirc[i++]);
+ else num.prepend('0');
+ ins = QString("%Dcf%1").arg(num);
+
+ if(i+1 < l && mirc[i] == ',' && mirc[i+1].isDigit()) {
+ i++;
+ num = mirc[i++];
+ if(i < l && mirc[i].isDigit()) num.append(mirc[i++]);
+ else num.prepend('0');
+ ins += QString("%Dcb%1").arg(num);
+ }
+ } else {
+ ins = "%Dc-";
+ }
+ mirc.replace(pos, i-pos, ins);
+ }
+ return mirc;
+}
+
+UiStyle::StyledMessage UiStyle::styleMessage(const Message &msg) {
+ QString user = userFromMask(msg.sender());
+ QString host = hostFromMask(msg.sender());
+ QString nick = nickFromMask(msg.sender());
+ QString txt = mircToInternal(msg.text());
+ QString bufferName = msg.bufferInfo().bufferName();
+
+ StyledMessage result;
+
+ result.timestamp = styleString(tr("%DT[%1]").arg(msg.timestamp().toLocalTime().toString("hh:mm:ss")));
+
+ QString s, t;
+ switch(msg.type()) {
+ case Message::Plain:
+ s = tr("%DS<%1>").arg(nick); t = tr("%D0%1").arg(txt); break;
+ case Message::Notice:
+ s = tr("%Dn[%1]").arg(nick); t = tr("%Dn%1").arg(txt); break;
+ case Message::Server:
+ s = tr("%Ds*"); t = tr("%Ds%1").arg(txt); break;
+ case Message::Error:
+ s = tr("%De*"); t = tr("%De%1").arg(txt); break;
+ case Message::Join:
+ s = tr("%Dj-->"); t = tr("%Dj%DN%1%DN %DH(%2@%3)%DH has joined %DC%4%DC").arg(nick, user, host, bufferName); break;
+ case Message::Part:
+ s = tr("%Dp<--"); t = tr("%Dp%DN%1%DN %DH(%2@%3)%DH has left %DC%4%DC").arg(nick, user, host, bufferName);
+ if(!txt.isEmpty()) t = QString("%1 (%2)").arg(t).arg(txt);
+ break;
+ case Message::Quit:
+ s = tr("%Dq<--"); t = tr("%Dq%DN%DU%1%DU%DN %DH(%2@%3)%DH has quit").arg(nick, user, host);
+ if(!txt.isEmpty()) t = QString("%1 (%2)").arg(t).arg(txt);
+ break;
+ case Message::Kick:
+ { s = tr("%Dk<-*");
+ QString victim = txt.section(" ", 0, 0);
+ //if(victim == ui.ownNick->currentText()) victim = tr("you");
+ QString kickmsg = txt.section(" ", 1);
+ t = tr("%Dk%DN%1%DN has kicked %DN%2%DN from %DC%3%DC").arg(nick).arg(victim).arg(bufferName);
+ if(!kickmsg.isEmpty()) t = QString("%1 (%2)").arg(t).arg(kickmsg);
+ }
+ break;
+ case Message::Nick:
+ s = tr("%Dr<->");
+ if(nick == msg.text()) t = tr("%DrYou are now known as %DN%1%DN").arg(txt);
+ else t = tr("%Dr%DN%1%DN is now known as %DN%2%DN").arg(nick, txt);
+ break;
+ case Message::Mode:
+ s = tr("%Dm***");
+ if(nick.isEmpty()) t = tr("%DmUser mode: %DM%1%DM").arg(txt);
+ else t = tr("%DmMode %DM%1%DM by %DN%2%DN").arg(txt, nick);
+ break;
+ case Message::Action:
+ s = tr("%Da-*-");
+ t = tr("%Da%DN%1%DN %2").arg(nick).arg(txt);
+ break;
+ default:
+ s = tr("%De%1").arg(msg.sender());
+ t = tr("%De[%1]").arg(txt);
+ }
+ result.sender = styleString(s);
+ result.text = styleString(t);
+ return result;
+}
+
+QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList) {
+ out << formatList.count();
+ UiStyle::FormatList::const_iterator it = formatList.begin();
+ while(it != formatList.end()) {
+ out << (*it).first << (*it).second;
+ ++it;
+ }
+ return out;
+}
+
+QDataStream &operator>>(QDataStream &in, UiStyle::FormatList &formatList) {
+ int cnt;
+ in >> cnt;
+ for(int i = 0; i < cnt; i++) {
+ int pos; quint32 ftype;
+ in >> pos >> ftype;
+ formatList.append(qMakePair(pos, ftype));
+ }
+ return in;
+}
# include "old-uistyle.h"
#else
+#include <QDataStream>
#include <QTextCharFormat>
#include <QTextLayout>
#include <QUrl>
#include "settings.h"
class UiStyle {
+ Q_DECLARE_TR_FUNCTIONS (UiStyle);
public:
UiStyle(const QString &settingsKey);
virtual ~UiStyle();
+ typedef QList<QPair<int, quint32> > FormatList;
+
//! This enumerates the possible formats a text element may have. */
/** These formats are ordered on increasing importance, in cases where a given property is specified
* by multiple active formats.
struct StyledString {
QString text;
- QList<QPair<int, quint32> > formats; // starting pos, ftypes
+ FormatList formats; // starting pos, ftypes
+ };
+
+ struct StyledMessage {
+ StyledString timestamp;
+ StyledString sender;
+ StyledString text;
};
StyledString styleString(const QString &);
+ StyledMessage styleMessage(const Message &);
void setFormat(FormatType, QTextCharFormat, Settings::Mode mode/* = Settings::Custom*/);
QTextCharFormat format(FormatType, Settings::Mode mode = Settings::Custom) const;
private:
+ QString mircToInternal(const QString &);
+
QTextCharFormat _defaultPlainFormat;
QHash<FormatType, QTextCharFormat> _defaultFormats;
QHash<FormatType, QTextCharFormat> _customFormats;
QString _settingsKey;
};
+QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList);
+QDataStream &operator>>(QDataStream &in, UiStyle::FormatList &formatList);
+
+Q_DECLARE_METATYPE(UiStyle::FormatList);
+
#endif // SPUTDEV
#endif