Qt 4.8
Public Types | Public Functions | Private Types | Properties | List of all members
QPatternist::ColorOutput Class Reference

#include <qcoloroutput_p.h>

Inheritance diagram for QPatternist::ColorOutput:
QPatternist::ColoringMessageHandler

Public Types

typedef QFlags< ColorCodeComponentColorCode
 
enum  ColorCodeComponent {
  BlackForeground = 1 << ForegroundShift, BlueForeground = 2 << ForegroundShift, GreenForeground = 3 << ForegroundShift, CyanForeground = 4 << ForegroundShift,
  RedForeground = 5 << ForegroundShift, PurpleForeground = 6 << ForegroundShift, BrownForeground = 7 << ForegroundShift, LightGrayForeground = 8 << ForegroundShift,
  DarkGrayForeground = 9 << ForegroundShift, LightBlueForeground = 10 << ForegroundShift, LightGreenForeground = 11 << ForegroundShift, LightCyanForeground = 12 << ForegroundShift,
  LightRedForeground = 13 << ForegroundShift, LightPurpleForeground = 14 << ForegroundShift, YellowForeground = 15 << ForegroundShift, WhiteForeground = 16 << ForegroundShift,
  BlackBackground = 1 << BackgroundShift, BlueBackground = 2 << BackgroundShift, GreenBackground = 3 << BackgroundShift, CyanBackground = 4 << BackgroundShift,
  RedBackground = 5 << BackgroundShift, PurpleBackground = 6 << BackgroundShift, BrownBackground = 7 << BackgroundShift, DefaultColor = 1 << SpecialShift
}
 
typedef QHash< int, ColorCodeColorMapping
 

Public Functions

QString colorify (const QString &message, int color=-1) const
 Treats message and colorID identically to write(), but instead of writing message to stderr, it is prepared for being written to stderr, but is then returned. More...
 
ColorMapping colorMapping () const
 Returns the color mappings in use. More...
 
 ColorOutput ()
 Constructs a ColorOutput instance, ready for use. More...
 
void insertMapping (int colorID, const ColorCode colorCode)
 Adds a color mapping from colorID to colorCode, for this ColorOutput instance. More...
 
void setColorMapping (const ColorMapping &cMapping)
 Sets the color mapping to be cMapping. More...
 
void write (const QString &message, int color=-1)
 Sends message to stderr, using the color looked up in colorMapping() using colorID. More...
 
void writeUncolored (const QString &message)
 Writes message to stderr as if for instance QTextStream would have been used, and adds a line ending at the end. More...
 
 ~ColorOutput ()
 Destructs this ColorOutput instance. More...
 

Private Types

enum  {
  ForegroundShift = 10, BackgroundShift = 20, SpecialShift = 20, ForegroundMask = ((1 << ForegroundShift) - 1) << ForegroundShift,
  BackgroundMask = ((1 << BackgroundShift) - 1) << BackgroundShift
}
 

Properties

ColorOutputPrivated
 

Detailed Description

Definition at line 66 of file qcoloroutput_p.h.

Typedefs

◆ ColorCode

Definition at line 107 of file qcoloroutput_p.h.

◆ ColorMapping

Definition at line 108 of file qcoloroutput_p.h.

Enumerations

◆ anonymous enum

anonymous enum
private

◆ ColorCodeComponent

Enumerator
BlackForeground 
BlueForeground 
GreenForeground 
CyanForeground 
RedForeground 
PurpleForeground 
BrownForeground 
LightGrayForeground 
DarkGrayForeground 
LightBlueForeground 
LightGreenForeground 
LightCyanForeground 
LightRedForeground 
LightPurpleForeground 
YellowForeground 
WhiteForeground 
BlackBackground 
BlueBackground 
GreenBackground 
CyanBackground 
RedBackground 
PurpleBackground 
BrownBackground 
DefaultColor 

Definition at line 78 of file qcoloroutput_p.h.

79  {
96 
105  };

Constructors and Destructors

◆ ColorOutput()

ColorOutput::ColorOutput ( )

Constructs a ColorOutput instance, ready for use.

Definition at line 271 of file qcoloroutput.cpp.

271  : d(new ColorOutputPrivate())
272 {
273 }
ColorOutputPrivate * d

◆ ~ColorOutput()

ColorOutput::~ColorOutput ( )

Destructs this ColorOutput instance.

Definition at line 278 of file qcoloroutput.cpp.

279 {
280  delete d;
281 }
ColorOutputPrivate * d

Functions

◆ colorify()

QString ColorOutput::colorify ( const QString message,
int  colorID = -1 
) const

Treats message and colorID identically to write(), but instead of writing message to stderr, it is prepared for being written to stderr, but is then returned.

This is useful when the colored string is inserted into a translated string(dividing the string into several small strings prevents proper translation).

Definition at line 319 of file qcoloroutput.cpp.

Referenced by QPatternist::ColoringMessageHandler::colorifyDescription(), QPatternist::ColoringMessageHandler::handleMessage(), and write().

320 {
321  Q_ASSERT_X(colorID == -1 || d->colorMapping.contains(colorID), Q_FUNC_INFO,
322  qPrintable(QString::fromLatin1("There is no color registered by id %1").arg(colorID)));
323  Q_ASSERT_X(!message.isEmpty(), Q_FUNC_INFO, "It makes no sense to attempt to print an empty string.");
324 
325  if(colorID != -1)
326  d->currentColorID = colorID;
327 
328  if(d->coloringEnabled && colorID != -1)
329  {
330  const int color(d->colorMapping.value(colorID));
331 
332  /* If DefaultColor is set, we don't want to color it. */
333  if(color & DefaultColor)
334  return message;
335 
336  const int foregroundCode = (int(color) & ForegroundMask) >> ForegroundShift;
337  const int backgroundCode = (int(color) & BackgroundMask) >> BackgroundShift;
338  QString finalMessage;
339  bool closureNeeded = false;
340 
341  if(foregroundCode)
342  {
344  closureNeeded = true;
345  }
346 
347  if(backgroundCode)
348  {
350  closureNeeded = true;
351  }
352 
353  finalMessage.append(message);
354 
355  if(closureNeeded)
356  {
357  finalMessage.append(QChar(0x1B));
358  finalMessage.append(QLatin1String("[0m"));
359  }
360 
361  return finalMessage;
362  }
363  else
364  return message;
365 }
static const char *const foregrounds[]
static const char *const backgrounds[]
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool contains(const Key &key) const
Returns true if the hash contains an item with the key; otherwise returns false.
Definition: qhash.h:872
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
static QString escapeCode(const QString &in)
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
ColorOutputPrivate * d
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
QString & append(QChar c)
Definition: qstring.cpp:1777
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
ColorOutput::ColorMapping colorMapping
#define qPrintable(string)
Definition: qglobal.h:1750
#define Q_FUNC_INFO
Definition: qglobal.h:1871

◆ colorMapping()

ColorOutput::ColorMapping ColorOutput::colorMapping ( ) const

Returns the color mappings in use.

See also
setColorMapping(), insertMapping()

Definition at line 263 of file qcoloroutput.cpp.

264 {
265  return d->colorMapping;
266 }
ColorOutputPrivate * d
ColorOutput::ColorMapping colorMapping

◆ insertMapping()

void ColorOutput::insertMapping ( int  colorID,
const ColorCode  colorCode 
)

Adds a color mapping from colorID to colorCode, for this ColorOutput instance.

This is a convenience function for creating a ColorOutput::ColorMapping instance and calling setColorMapping().

See also
colorMapping(), setColorMapping()

Definition at line 375 of file qcoloroutput.cpp.

Referenced by QPatternist::ColoringMessageHandler::ColoringMessageHandler().

376 {
377  d->colorMapping.insert(colorID, colorCode);
378 }
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
ColorOutputPrivate * d
ColorOutput::ColorMapping colorMapping

◆ setColorMapping()

void ColorOutput::setColorMapping ( const ColorMapping cMapping)

Sets the color mapping to be cMapping.

Negative values are disallowed.

See also
colorMapping(), insertMapping()

Definition at line 253 of file qcoloroutput.cpp.

254 {
255  d->colorMapping = cMapping;
256 }
ColorOutputPrivate * d
ColorOutput::ColorMapping colorMapping

◆ write()

void ColorOutput::write ( const QString message,
int  colorID = -1 
)

Sends message to stderr, using the color looked up in colorMapping() using colorID.

If color isn't available in colorMapping(), result and behavior is undefined.

If colorID is 0, which is the default value, the previously used coloring is used. ColorOutput is initialized to not color at all.

If message is empty, effects are undefined.

message will be printed as is. For instance, no line endings will be inserted.

Definition at line 295 of file qcoloroutput.cpp.

296 {
297  d->write(colorify(message, colorID));
298 }
QString colorify(const QString &message, int color=-1) const
Treats message and colorID identically to write(), but instead of writing message to stderr...
void write(const QString &msg)
ColorOutputPrivate * d

◆ writeUncolored()

void ColorOutput::writeUncolored ( const QString message)

Writes message to stderr as if for instance QTextStream would have been used, and adds a line ending at the end.

This function can be practical to use such that one can use ColorOutput for all forms of writing.

Definition at line 306 of file qcoloroutput.cpp.

Referenced by QPatternist::ColoringMessageHandler::handleMessage().

307 {
308  d->write(message + QLatin1Char('\n'));
309 }
void write(const QString &msg)
ColorOutputPrivate * d
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

Properties

◆ d

ColorOutputPrivate* QPatternist::ColorOutput::d
private

Definition at line 122 of file qcoloroutput_p.h.

Referenced by colorify(), insertMapping(), write(), writeUncolored(), and ~ColorOutput().


The documentation for this class was generated from the following files: