]> Shamusworld >> Repos - ttedit/blob - src/toolwindow.cpp
fda5b8e7c204a961d1a66692155f997ae462e567
[ttedit] / src / toolwindow.cpp
1 //
2 // TOOLWINDOW.CPP - Tool Palette window
3 // by James L. Hammons
4 // (C) 2008 Underground Software
5 //
6 // JLH = James L. Hammons <jlhamm@acm.org>
7 //
8 // Who  When        What
9 // ---  ----------  -----------------------------------------------------------
10 // JLH  08/28/2008  Created this file
11 // JLH  03/11/2009  Converted from wxWidgets to Qt
12 //
13
14 // FIXED:
15 //
16 // STILL TO BE DONE:
17 //
18 //
19
20 // Uncomment this for debugging...
21 #define DEBUG
22 #define DEBUGFOO            // Various tool debugging...
23 #define DEBUGTP                         // Toolpalette debugging...
24
25 #include "toolwindow.h"
26
27
28 ToolWindow::ToolWindow(void): QWidget(NULL, Qt::Window | Qt::FramelessWindowHint),
29         prevTool(TOOLNone)
30 {
31 //      img = QImage(":/res/toolpal1.xpm");
32         img = QImage(":/res/toolpal1.png");
33
34         // Set up sizes
35
36         sizeTPBM.rx() = img.width(), sizeTPBM.ry() = img.height();
37         sizeStamp.rx() = sizeTPBM.x() / 4, sizeStamp.ry() = sizeTPBM.y() / 3;
38
39         setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
40         setVisible(false);
41 }
42
43 QSize ToolWindow::sizeHint() const
44 {
45         return QSize(sizeTPBM.x(), sizeTPBM.y());
46 }
47
48 void ToolWindow::paintEvent(QPaintEvent * event)
49 {
50         QPainter p(this);
51         p.drawImage(QPoint(0, 0), img);
52
53         if (prevTool != -1)
54         {
55             //need ul corner of bitmap, ul corner of dest, width/height
56                 p.setCompositionMode(QPainter::RasterOp_NotSource);
57 //              QPoint pt(sizeStamp.x() * (prevTool & 0x03), sizeStamp.y() * (prevTool >> 2));
58                 QPoint pt(sizeStamp.x() * (prevTool % 4), sizeStamp.y() * (prevTool / 4));
59                 p.drawImage(pt.x(), pt.y(), img, pt.x(), pt.y(), sizeStamp.x(), sizeStamp.y());
60         }
61 }
62
63 //
64 // Find which tool we're pointing at
65 //
66 ToolType ToolWindow::FindSelectedTool(void)
67 {
68         // Get mouse coords relative to the tool palette window
69 //      wxPoint pt = ScreenToClient(wxGetMousePosition());
70         QPoint pt = mapFromGlobal(QCursor::pos());
71 //printf("pt = %u, %u\n", pt.x(), pt.y());
72
73         // Divide mouse coords by the bitmap stamp size to find which one is pointed to
74         uint32_t x = (uint32_t)pt.x() / sizeStamp.x(), y = (uint32_t)pt.y() / sizeStamp.y();
75
76         // Preset failure into newTool, in case no new tool is selected
77         ToolType newTool = TOOLNone;
78
79         // NOTE: This works because x and y are UNSIGNED
80         if (x < 4 && y < 3)
81                 newTool = (ToolType)((y * 4) + x);
82
83         // We don't have 11 yet, so fix this if the user selected the blank space
84 //now we do!
85 //      if (newTool > 10)
86 //              newTool = TOOLNone;
87
88         return newTool;
89 }
90