OSDN Git Service

implement caret drawing for some types
authorsuma <devnull@localhost>
Sun, 8 Feb 2009 18:22:20 +0000 (03:22 +0900)
committersuma <devnull@localhost>
Sun, 8 Feb 2009 18:22:20 +0000 (03:22 +0900)
src/control/standard/hexview.cpp
src/control/standard/hexview.h
src/control/standard/scursor.cpp
src/control/standard/scursor.h

index f28d1d1..97d212f 100644 (file)
@@ -27,7 +27,7 @@ HexConfig::HexConfig()
        Colors[Color::SelBackground] = QColor(0xCC,0xCC,0xFF);
        Colors[Color::SelText] = QColor(0,0x40,0x40);
        Colors[Color::CaretBackground] = QColor(0xFF, 0, 0);
-       Colors[Color::CaretText] = QColor(0xAA,0xAA,0x20);
+       Colors[Color::CaretText] = QColor(0xFA,0xFA,0x20);
 
        // Font
        Font.setFixedPitch(true);
@@ -278,9 +278,9 @@ inline void HexView::drawText(QPainter &painter, const QString &hex, int x, int
        painter.drawText(x, y, config.charWidth(2), config.charHeight(), Qt::AlignCenter, hex);
 }
 
-void HexView::drawCaret(bool visible, quint64 position, int height_max)
+void HexView::drawCaret(bool visible, quint64 pos, int height_max)
 {
-       qDebug("drawCaret visible:%d height_max:%d sel:%llu top:%llu", visible, height_max, position, cur_->Top);
+       qDebug("drawCaret visible:%d height_max:%d sel:%llu top:%llu", visible, height_max, pos, cur_->Top);
 
        if (!(config.top() + config.byteHeight() < height_max)) {
                return;
@@ -288,35 +288,111 @@ void HexView::drawCaret(bool visible, quint64 position, int height_max)
 
        QPainter painter(&pix_);
        painter.setFont(config.Font);
-       const int line = position / HexConfig::Num - cur_->Top;
-       const int x = position % HexConfig::Num;
+       const int line = pos / HexConfig::Num - cur_->Top;
+       const int x = pos % HexConfig::Num;
        const int y = config.top() + config.byteHeight() * line;
 
-       int caret_color = Color::CaretBackground;
-       if (doc_->length() <= position) {
-               // Draw background
-               QBrush brush(config.Colors[Color::CaretBackground]);
-               painter.fillRect(config.x(x), y, config.byteWidth(), config.byteHeight(), brush);
-       } else {
+       drawCaretShape(painter, pos, x, y, pos < doc_->length());
+
+       painter.end();
+       update(config.x(x), y, config.byteWidth(), config.charHeight());
+}
+
+void HexView::drawCaretShape(QPainter &painter, quint64 pos, int x, int y, bool drawText)
+{
+#define DrawTextNormally() { \
+       QBrush brush(config.Colors[Color::Background]); \
+       painter.setBackground(brush); \
+       painter.setPen(config.Colors[Color::Text]); \
+       painter.fillRect(config.x(x), y, config.byteWidth(), config.byteHeight(), brush); \
+       painter.drawText(config.x(x) + config.ByteMargin.left(), y + config.ByteMargin.top(), config.charWidth(2), config.charHeight(), Qt::AlignCenter, hex); \
+}
+
+#define DrawCaret_Line() { \
+       QBrush brush(config.Colors[Color::CaretBackground]); \
+       painter.fillRect(config.x(x) + (cur_->CaretHigh ? 0 : config.charWidth() + config.ByteMargin.left()), y, 2, config.byteHeight(), brush); \
+}
+
+#define DrawCaret_Underbar() { \
+       QBrush brush(config.Colors[Color::CaretBackground]); \
+       int width = config.charWidth() + (cur_->CaretHigh ? config.ByteMargin.left() : config.ByteMargin.right()); \
+       painter.fillRect(config.x(x) + (cur_->CaretHigh ? 0 : config.charWidth() + config.ByteMargin.left()), y + config.byteHeight() - 2, width, 2, brush); \
+}
+
+#define DrawCaret_Frame() { \
+               painter.setPen(config.Colors[Color::CaretBackground]); \
+               int width = cur_->CaretHigh ? config.byteWidth() : config.charWidth() + config.ByteMargin.right(); \
+               QRect rect(config.x(x) + (cur_->CaretHigh ? 0 : config.charWidth() + config.ByteMargin.left()), y, width, config.byteHeight()); \
+               painter.drawRect(rect); \
+}
+
+       if (drawText) {
                // Copy from document
                uchar data;
-               doc_->get(position, &data, 1);
+               doc_->get(pos, &data, 1);
 
                QString hex;
                hex.resize(2);
                byteToHex(data, hex);
 
-               // Draw
-               QBrush brush(config.Colors[Color::CaretBackground]);
-               painter.setBackground(brush);
-               painter.setPen(config.Colors[Color::CaretText]);
-               painter.fillRect(config.x(x), y, config.byteWidth(), config.byteHeight(), brush);
-
-               painter.drawText(config.x(x) + config.ByteMargin.left(), y + config.ByteMargin.top(), config.charWidth(2), config.charHeight(), Qt::AlignCenter, hex);
+               DrawTextNormally();
+               DrawCaret_Underbar();
+               return;
+               switch (cur_->CaretShape) {
+               case CARET_LINE:
+                       DrawTextNormally();
+                       DrawCaret_Line();
+                       break;
+               case CARET_BLOCK:
+                       if (cur_->CaretHigh) {
+                               QBrush brush(config.Colors[Color::CaretBackground]);
+                               painter.setBackground(brush);
+                               painter.setPen(config.Colors[Color::CaretText]);
+                               painter.fillRect(config.x(x), y, config.byteWidth(), config.byteHeight(), brush);
+                               painter.drawText(config.x(x) + config.ByteMargin.left(), y + config.ByteMargin.top(), config.charWidth(2), config.charHeight(), Qt::AlignCenter, hex);
+                       } else {
+                               DrawTextNormally();
+                               QBrush brush(config.Colors[Color::CaretBackground]);
+                               painter.setBackground(brush);
+                               painter.setPen(config.Colors[Color::CaretText]);
+                               painter.fillRect(config.x(x) + config.ByteMargin.left() + config.charWidth(), y, config.charWidth() + config.ByteMargin.right(), config.byteHeight(), brush);
+                               hex.remove(0, 1);
+                               painter.drawText(config.x(x) + config.ByteMargin.left() + config.charWidth(), y + config.ByteMargin.top(), config.charWidth(2), config.charHeight(), Qt::AlignLeft, hex);
+                       }
+                       break;
+               case CARET_FRAME:
+                       DrawTextNormally();
+                       DrawCaret_Frame();
+                       break;
+               case CARET_UNDERBAR:
+                       DrawTextNormally();
+                       DrawCaret_Underbar();
+                       break;
+               }
+       } else {
+               switch (cur_->CaretShape) {
+               case CARET_LINE:
+                       DrawCaret_Line();
+                       break;
+               case CARET_BLOCK:
+               {
+                       QBrush brush(config.Colors[Color::CaretBackground]);
+                       painter.fillRect(config.x(x), y, config.byteWidth(), config.byteHeight(), brush);
+                       break;
+               }
+               case CARET_FRAME:
+                       DrawCaret_Frame();
+                       break;
+               case CARET_UNDERBAR:
+                       DrawCaret_Underbar();
+                       break;
+               }
        }
 
-       painter.end();
-       update(config.x(x), y, config.byteWidth(), config.charHeight());
+
+       return;
+
+#undef DrawTextNormally
 }
 
 void HexView::drawCaret(bool visible)
@@ -521,5 +597,11 @@ void HexView::keyPressEvent(QKeyEvent *ev)
        }
 }
 
+HexView::CaretDrawInfo::CaretDrawInfo(QPainter &p, int x, int y)
+       : painter(p)
+{
+       this->x = x;
+       this->y = y;
+}
 
 }      // namespace
index 385c175..25c5fd2 100644 (file)
@@ -106,6 +106,17 @@ namespace Standard {
                void setCaretBlink(bool enable);
 
        protected:
+               class CaretDrawInfo
+               {
+                       public:
+                               CaretDrawInfo(QPainter &, int x, int y);
+                               QPainter &painter;
+                               QString hex;
+                               int x;
+                               int y;
+               };
+
+       protected:
 
                void refreshPixmap();
                void refreshPixmap(int type, int = 0, int = 0);
@@ -124,6 +135,7 @@ namespace Standard {
                bool isSelected(quint64 pos);
                void drawSelected(bool reset = false);
                void drawCaret(bool visible, quint64 pos, int ymax);
+               void drawCaretShape(QPainter &painter, quint64 pos, int x, int y, bool);
                void redrawCaret();
 
                void byteToHex(uchar c, QString &h);
index edc6b93..54a7560 100644 (file)
@@ -19,6 +19,7 @@ Cursor::Cursor(Document *Doc, HexView *View)
        , Selected(false)
        , Toggle(false)
        , CaretHigh(true)
+       , CaretShape(CARET_BLOCK)
        , CaretTimerId(0)
        , HexCaretVisible(true)
        , StrCaretVisible(true)
index 2088a1f..12982d0 100644 (file)
@@ -13,6 +13,13 @@ namespace Standard {
                quint64 X, Y;
        };
 
+       enum CaretShape {
+               CARET_LINE,
+               CARET_BLOCK,
+               CARET_FRAME,
+               CARET_UNDERBAR,
+       };
+
        class Cursor
        {
        public:
@@ -37,8 +44,10 @@ namespace Standard {
                bool Selected;          // Begin != End
                bool Toggle;
                bool CaretHigh;
-
-               int CaretTimerId;       // for Caret
+       
+               // for Caret
+               int CaretShape;
+               int CaretTimerId;
                bool HexCaretVisible;
                bool StrCaretVisible;