1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
// render2d_text.cpp - text-editing functions in class Render2D
#include <iostream.h>
#include <math.h>
#include <qwidget.h>
#include <qbitmap.h>
#include <qpixmap.h>
#include <qcursor.h>
#include <qpainter.h>
#include <qfontmetrics.h>
#include <qtextstream.h>
#include "render2d.h"
#include "chemdata.h"
#include "text.h"
#include "defs.h"
// mouse events to handle text functions
// (button states should be set before we get here)
void Render2D::DrawText_mousePressEvent(QMouseEvent *e1) {
text_drag = false;
if (localtext != 0) {
DPoint *e = new DPoint;
e->x = e1->x(); e->y = e1->y();
if (localtext->WithinBounds(e) == true) {
// move cursor, select, or something...
text_drag = true;
start_drag = e;
}
}
// do nothing, wait for release
}
void Render2D::DrawText_mouseReleaseEvent(QMouseEvent *e1) {
if (localtext != 0) { // if already working on something, change behavior
DPoint *e = new DPoint;
e->x = e1->x(); e->y = e1->y();
if (text_drag == true) {
double dlta = fabs((start_drag->x - e->x) + (start_drag->y - e->y));
if (dlta < 0.5) {
localtext->MoveCursor(e);
text_drag = false;
repaint();
return;
}
// finish multiple selection
localtext->Select(start_drag, e);
repaint();
text_drag = false;
return;
}
if (localtext->WithinBounds(e) == true) {
// move cursor, select, or something...
} else {
// save text, clean up
localtext->DeselectAllText();
emit TextOff();
if (!text_exists) {
if (localtext->getText().length() > 0)
{ c->addText(localtext); localtext = 0; }
} else {
if (localtext->getText().length() == 0)
{ c->Erase(localtext); localtext = 0; }
}
localtext = 0;
highlightpoint = 0;
if (highlightobject != 0) {
highlightobject->Highlight(false);
highlightobject = 0;
}
repaint();
}
return;
}
if (highlightobject != 0) { // edit existing object
text_exists = true;
localtext = (Text *)highlightobject;
emit TextOn(localtext->getFont());
repaint();
} else { // create new object
text_exists = false;
localtext = new Text(this);
localtext->setFont(currentFont);
localtext->SetColor(currentColor);
DPoint *e = new DPoint;
e->x = e1->x(); e->y = e1->y();
emit TextOn(localtext->getFont());
if (highlightpoint) {
localtext->setPoint(highlightpoint);
localtext->setJustify(JUSTIFY_CENTER);
} else {
localtext->setPoint(e);
localtext->setJustify(JUSTIFY_TOPLEFT);
}
repaint();
}
}
void Render2D::DrawText_mouseMoveEvent(QMouseEvent *e1) {
//bool update;
DPoint *prevhighlight = highlightpoint;
Drawable *prevhighlightobject = highlightobject;
// Create DPoint of current pointer position
DPoint *e = new DPoint;
DPoint *np = 0;
e->x = e1->x(); e->y = e1->y();
double dist, distobj;
// Get DPoint of nearest point
np = c->FindNearestPoint(e, dist);
// get Drawable of nearest object
Drawable *no = c->FindNearestObject(e, distobj);
if (localtext != 0) { // handle moves when there is a current object
if (text_drag == true) {
localtext->Select(start_drag, e);
repaint();
}
return;
}
// look for place to draw if no current text object
if (no != 0) {
if (no->Type() == TYPE_TEXT) { // highlight text if closest
highlightpoint = 0;
highlightobject = no;
if (prevhighlightobject != 0) prevhighlightobject->Highlight(false);
highlightobject->Highlight(true);
if (prevhighlightobject != highlightobject) repaint();
return;
} else { // deselect and check for points
// Clear highlighted object
highlightobject = 0;
if (prevhighlightobject != 0) prevhighlightobject->Highlight(false);
if (prevhighlightobject != highlightobject) repaint();
}
}
// clear highlighted object (if any)
if (prevhighlightobject != 0) {
prevhighlightobject->Highlight(false);
highlightobject = 0;
repaint();
}
// check points
if (dist < 6.0) {
highlightpoint = np;
if (prevhighlight != highlightpoint) repaint();
return;
}
if (dist >= 6.0) {
// Clear highlighted point
highlightpoint = 0;
if (prevhighlight != highlightpoint) repaint();
return;
}
}
// Handle keypress event here, since it only really applies to text
void Render2D::keyPressEvent(QKeyEvent *k) {
if (mode != MODE_TEXT) return;
// if text object is active, do something.
if (localtext != 0) {
// if escape character pressed, save object
if (k->key() == Key_Escape) {
emit TextOff();
if (!text_exists) {
if (localtext->getText().length() > 0)
c->addText(localtext);
} else {
if (localtext->getText().length() == 0)
c->Erase(localtext);
}
localtext = 0;
highlightpoint = 0;
if (highlightobject != 0) {
highlightobject->Highlight(false);
highlightobject = 0;
}
repaint();
return;
}
// if return pressed, and Justify == JUSTIFY_CENTER...
if ( (k->key() == Key_Return) &&
(localtext->Justify() == JUSTIFY_CENTER) ) {
emit TextOff();
if (!text_exists) {
if (localtext->getText().length() > 0)
c->addText(localtext);
} else {
if (localtext->getText().length() == 0)
c->Erase(localtext);
}
localtext = 0;
highlightpoint = 0;
if (highlightobject != 0) {
highlightobject->Highlight(false);
highlightobject = 0;
}
repaint();
return;
}
// if not escape, just insert.
localtext->InsertCharacter(k);
repaint();
}
}
// Superscript selected text
void Render2D::Superscript() {
if (localtext != 0) {
localtext->DoSuperscript();
repaint();
}
}
// Subscript selected text
void Render2D::Subscript() {
if (localtext != 0) {
localtext->DoSubscript();
repaint();
}
}
// Bold selected text
void Render2D::Bold() {
if (localtext != 0) {
localtext->DoBold();
repaint();
}
}
// Italicize selected text
void Render2D::Italic() {
if (localtext != 0) {
localtext->DoItalic();
repaint();
}
}
// Underline selected text
void Render2D::Underline() {
if (localtext != 0) {
localtext->DoUnderline();
repaint();
}
}
// origin = origin point of text
// intext = text to draw (or blank); return if modified or not
// justify = how to arrange text around origin
// oneline = if true (i.e., a label), it's a one-line widget
QString Render2D::EditText(QPoint origin, QString intext, int justify,
bool oneline)
{
return intext;
}
int Render2D::GetTextHeight(QFont fn) {
QPainter p(this);
p.setFont(fn);
QFontMetrics fm = p.fontMetrics();
return fm.ascent();
}
int Render2D::GetTextFullHeight(QFont fn) {
QPainter p(this);
p.setFont(fn);
QFontMetrics fm = p.fontMetrics();
return fm.height();
}
int Render2D::GetCharWidth(QChar ch, QFont fn) {
QPainter p(this);
p.setFont(fn);
QFontMetrics fm = p.fontMetrics();
return fm.width(ch);
}
int Render2D::GetStringWidth(QString ch, QFont fn) {
QPainter p(this);
p.setFont(fn);
QFontMetrics fm = p.fontMetrics();
return fm.width(ch);
}
QRect Render2D::GetTextDimensions(QString txt, QFont fn) {
QRect retval;
// if empty, return box large enough to hold cursor and indicate place.
if (txt.isNull() || txt.isEmpty()) {
}
QPainter p(this);
p.setFont(fn);
int maxwidth, lwidth, linecount, height;
QFontMetrics fm = p.fontMetrics();
QTextStream t(txt, IO_ReadOnly);
linecount = 1;
maxwidth = 0;
QString l;
// find maximum width
do {
l = t.readLine();
lwidth = fm.width(l);
if (lwidth > maxwidth) maxwidth = lwidth;
} while (!t.atEnd());
// find height
for (int i = 0; i < txt.length(); i++) {
if ((int)QChar(txt[i]) == 10) linecount++;
}
if (linecount < 1) linecount = 1;
if (maxwidth < 5) maxwidth = 5;
height = linecount * fm.height();
retval.setWidth(maxwidth);
retval.setHeight(height);
return retval;
}
|