Results 1 to 17 of 17

Thread: Scaling

  1. #1
    Join Date
    Jun 2008
    Location
    Saint-Petersburg
    Posts
    50
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Scaling

    Hi!
    How to do scaling of my graphics by Qt means?

    Thanks for helps!

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Scaling

    What graphics? Be more specific, please!

  3. #3
    Join Date
    Jun 2008
    Location
    Saint-Petersburg
    Posts
    50
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Scaling

    Quote Originally Posted by caduel View Post
    What graphics? Be more specific, please!
    Graphics of dependence one parameter in other parameter! It is not important!
    The dialog that is shown this graphic with grid! What do you know some more?

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Scaling

    Assuming that you are showing an image, use QImage::scaled, if your graphics are in the form of a QPixmap, use QPixmap::scaled...

    If that is not what you would like to know:
    give us more information! Your question is not really precise and I am wondering what you are trying to do. Hard to give advice that way.

    If you would give us a few lines of your code and say what is not working the way you want it to, you would save us time and get more helpful answers.

    HTH

  5. #5
    Join Date
    Jun 2008
    Location
    Saint-Petersburg
    Posts
    50
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Scaling

    Quote Originally Posted by caduel View Post
    Assuming that you are showing an image, use QImage::scaled, if your graphics are in the form of a QPixmap, use QPixmap::scaled...

    If that is not what you would like to know:
    give us more information! Your question is not really precise and I am wondering what you are trying to do. Hard to give advice that way.

    If you would give us a few lines of your code and say what is not working the way you want it to, you would save us time and get more helpful answers.

    HTH
    I understand you My code that became out of date (Manual scaling, not Qt)! Tommorow I'll can give my new code! Concrete questions will appear tommorow! Thanks.

  6. #6
    Join Date
    Jun 2008
    Location
    Saint-Petersburg
    Posts
    50
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Scaling

    I want to draw grid. I have minimal, maximal means, step. Should I specify QMatrix? How to do it? What should I do it?

    My previous code was:
    Qt Code:
    1. void QModeGraphics::drawGrid(QPainter *painter)
    2. {
    3. QRect rect(modesDisplay -> rect());
    4. if(!rect.isValid()) return;
    5.  
    6. QRect boundString;
    7. /// Отрисовка осей вдоль оси X
    8. for(int i=0; i<=settings.numXTicks; ++i)
    9. {
    10. int x = rect.left() + (i * (rect.width() - 1) / settings.numXTicks);
    11. double label = (settings.minX >= 0) ?
    12. settings.minX + (i * settings.spanX() / settings.numXTicks)
    13. : settings.maxX - (i * settings.spanX() / settings.numXTicks);
    14. QString s_label = GraphicDisplay::round(label);
    15.  
    16. painter -> setPen(Qt::black);
    17. painter -> drawLine(x, rect.top(), x, rect.bottom());
    18. if(i != settings.numXTicks)
    19. {
    20. int flags = Qt::AlignHCenter | Qt::AlignTop;
    21. boundString = painter -> boundingRect(boundString, flags, s_label);
    22. painter -> drawText(x - (boundString.width() + 5), rect.bottom() - (boundString.height() + 5),
    23. boundString.width(), boundString.height(), flags, s_label);
    24. }
    25. }
    26. if(mode != NULL)
    27. {
    28. int flags = Qt::AlignCenter;
    29. boundString = painter -> boundingRect(boundString, flags, mode -> x_param.param_name);
    30. painter -> drawText(rect.right() - boundString.width(), rect.bottom() - 2 * boundString.height(),
    31. mode -> x_param.param_name);
    32. }
    33. /// Отрисовка осей вдоль оси Y
    34. for(int j=0; j<=settings.numYTicks; ++j)
    35. {
    36. int y = rect.bottom() - (j * (rect.height() - 1) / settings.numYTicks);
    37. double label = settings.minY + (j * settings.spanY() / settings.numYTicks);
    38. QString s_label = GraphicDisplay::round(label);
    39.  
    40. painter -> setPen(Qt::black);
    41. painter -> drawLine(rect.left(), y, rect.right(), y);
    42. if(j != settings.numYTicks)
    43. {
    44. int flags = Qt::AlignRight | Qt::AlignTop;
    45. boundString = painter -> boundingRect(boundString, flags, s_label);
    46. painter -> drawText(rect.left() + 7, y - boundString.height(),
    47. boundString.width(), boundString.height(), flags, s_label);
    48. }
    49. }
    50. if(mode != NULL)
    51. {
    52. int flags = Qt::AlignCenter;
    53. boundString = painter -> boundingRect(boundString, flags, mode -> y_param.param_name);
    54. painter -> drawText(rect.left() + 7, rect.top() + boundString.height(),
    55. mode -> y_param.param_name);
    56. }
    57. painter -> drawRect(rect.adjusted(0, 0, -1, -1));
    58. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jun 2008
    Location
    Saint-Petersburg
    Posts
    50
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Scaling

    Help me, please! I don't know how to draw the grid that is scale with graphic! Don't ignore my posts, please!

  8. #8
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Scaling

    Well...

    first:
    * obviously I can't compile that example (not self contained)
    * I figure from your comment that this is working code, probable Qt3 based, right?
    * (I don't understand russian comments in kyrillian fonts, btw ;-)

    You should tell us what you want to do, what is your problem.
    From looking at your code, I would say that the same is possible in Qt4.
    (Why) do you want to change it?
    Is something not working/compiling?


    You only have to specify a matrix if you want to apply an affine transformation on the draw stuff. Scaling would be an example.
    So: if your problem is (only) that the grid is drawn in the wrong size, then yes: apply a scaling matrix to the painter.
    (Perhaps it would be in order to save() it first, and restore() it later.)

    HTH

  9. #9
    Join Date
    Jun 2008
    Location
    Saint-Petersburg
    Posts
    50
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Scaling

    Well.....
    My problem is seen so: I don't know how it is possible in Qt4! What do I can with means of step, min and max values? What element in Qt account for it? I want to draw grid and graphic by Qt and not manually!

    My previous code worked but it didn't scale right! I solved write this code by management Qt!
    Sorry with my english! I say with difficulties and lexicographer! So my native language is russian!
    Last edited by AD; 19th June 2008 at 09:16.

  10. #10
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Scaling

    As far as I know, there is no built-in stuff for drawing grids.
    You have to do it manually, i.e. code it yourself.

    But then, your code seems to be for drawing a grid...
    isn't it working?

  11. #11
    Join Date
    Jun 2008
    Location
    Saint-Petersburg
    Posts
    50
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Scaling

    But.... Why I haven't Qt means?
    In my manual code I don't know how to do right scaling!
    My graphic stretched but it didn't near/move away! I don't know right algorithm scaling!

    P.S. May I turn to Qt built-in stuff?

  12. #12
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Scaling

    Qt Code:
    1. //If your grid is at the wrong position, do
    2.  
    3. painter->save();
    4.  
    5. QPoint offset=...;
    6. painter->translate(offset),
    7.  
    8. // if the grid is the wrong size:
    9. painter->scale(zoomX, zoomY);
    10.  
    11.  
    12. // draw here
    13.  
    14. painter->restore();
    To copy to clipboard, switch view to plain text mode 

    Details, see QPainter::save, QPainter::scale, QPainter::translate, QPainter::restore.

  13. #13
    Join Date
    Jun 2008
    Location
    Saint-Petersburg
    Posts
    50
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Scaling

    Well! Thank you!
    Possible, it is more concrete if I give my code, isn't it? This code with small correct.
    Header
    Qt Code:
    1. class QModeGraphics: public QDialog, public Ui::modeGraphics
    2. {
    3. Q_OBJECT
    4.  
    5. private:
    6. PlotSettings settings;
    7. QVector<QPointF> curve_vec;
    8.  
    9. QPoint origin;
    10. bool rubberBandIsShown;
    11. MODEGRAPHICS* mode;
    12. QPainter painter;
    13.  
    14. private:
    15. void drawGrid(QPainter* painter);
    16. void drawCurves(QPainter* painter);
    17. void drawModeBorder(QPainter* painter);
    18. // MORE code =)
    19.  
    20. protected:
    21. void paintEvent(QPaintEvent* events);
    22. void wheelEvent(QWheelEvent* events);
    23. void mousePressEvent(QMouseEvent* events);
    24. void mouseMoveEvent(QMouseEvent* events);
    25. void mouseReleaseEvent(QMouseEvent* events);
    26. void resizeEvent(QResizeEvent* events) { QDialog::resizeEvent(events); update(); }
    27. void closeEvent(QCloseEvent* events) { QDialog::closeEvent(events); }
    28. void showEvent(QShowEvent* events) { QDialog::showEvent(events); }
    29.  
    30. public:
    31. QModeGraphics(QWidget *parent = 0);
    32. ~QModeGraphics();
    33. void setPlotSettings(const PlotSettings& sts) { settings = sts; update(); }
    34. void initCurve();
    35. void zoomIn(int delta);
    36. void zoomOut(int delta);
    37. };
    To copy to clipboard, switch view to plain text mode 

    Source
    Qt Code:
    1. /// Draw all
    2. void QModeGraphics::paintEvent(QPaintEvent* events)
    3. {
    4. painter.begin(this);
    5.  
    6. painter.setWindow(modesDisplay -> rect());
    7. painter.setFont(QFont("Tahoma", 8, Qt::SolidLine));
    8. painter.setPen(Qt::black);
    9.  
    10. initCurve();
    11. drawGrid(&painter);
    12. drawCurves(&painter);
    13. drawModeBorder(&painter);
    14.  
    15. painter.end();
    16. QDialog::paintEvent(events);
    17. }
    18.  
    19. void QModeGraphics::wheelEvent(QWheelEvent* events)
    20. {
    21. int numDegrees = events -> delta() / 8;
    22. int numTicks = numDegrees / 15;
    23.  
    24. if(numTicks > 0) zoomIn(numTicks);
    25. else zoomOut(numTicks);
    26.  
    27. update();
    28. }
    29.  
    30. void QModeGraphics::mousePressEvent(QMouseEvent* events)
    31. {
    32. QWidget::mousePressEvent(events);
    33.  
    34. QRect r;
    35. switch(events -> button())
    36. {
    37. case Qt::LeftButton: ///< если нажата левая кнопка мыши
    38. origin = events -> pos();
    39. rubberBandIsShown = true;
    40. setCursor(Qt::CrossCursor);
    41. r = QRect(origin, QSize());
    42. rubber -> setGeometry(r);
    43. rubber -> show();
    44. break;
    45. case Qt::RightButton: ///< если нажата правая кнопка мыши
    46. break;
    47. default:
    48. break;
    49. }
    50. }
    51.  
    52. void QModeGraphics::mouseMoveEvent(QMouseEvent* events)
    53. {
    54. if(rubberBandIsShown)
    55. {
    56. rubber -> setWindowOpacity(0.0);
    57. rubber -> setGeometry(QRect(origin, events -> pos()).normalized());
    58. }
    59.  
    60. update();
    61. }
    62.  
    63. /// Возвращение прежнего вида курсору и изменение масштаба
    64. void QModeGraphics::mouseReleaseEvent(QMouseEvent* events)
    65. {
    66. if(events -> button() == Qt::LeftButton && rubberBandIsShown)
    67. {
    68. rubberBandIsShown = false;
    69. unsetCursor();
    70.  
    71. QRect rect = rubber -> geometry().normalized();
    72. if(rect.width() < 10 || rect.height() < 10) return;
    73. PlotSettings prevSettings = settings;
    74. PlotSettings settings_temp;
    75. double sp_x = prevSettings.spanX();
    76. double sp_y = prevSettings.spanY();
    77. double dx = prevSettings.spanX() / (width() - 2 * MARGIN);
    78. double dy = prevSettings.spanY() / (height() - 2 * MARGIN);
    79. settings_temp.minX = prevSettings.minX + dx * rect.left();
    80. settings_temp.maxX = prevSettings.minX + dx * rect.right();
    81. settings_temp.minY = prevSettings.maxY - dy * rect.bottom();
    82. settings_temp.maxY = prevSettings.maxY - dy * rect.top();
    83. settings_temp.adjust();
    84. settings = settings_temp;
    85.  
    86. rubber -> hide();
    87. update();
    88. }
    89. }
    90.  
    91. /// Grid drawing
    92. void QModeGraphics::drawGrid(QPainter *painter)
    93. {
    94. QRect rect(modesDisplay -> rect());
    95. if(!rect.isValid()) return;
    96.  
    97. QRect boundString;
    98. /// Отрисовка осей вдоль оси X
    99. for(int i=0; i<=settings.numXTicks; ++i)
    100. {
    101. int x = rect.left() + (i * (rect.width() - 1) / settings.numXTicks);
    102. double label = (settings.minX >= 0) ?
    103. settings.minX + (i * settings.spanX() / settings.numXTicks)
    104. : settings.maxX - (i * settings.spanX() / settings.numXTicks);
    105. QString s_label = GraphicDisplay::round(label);
    106.  
    107. painter -> setPen(Qt::black);
    108. painter -> drawLine(x, rect.top(), x, rect.bottom());
    109. if(i != settings.numXTicks)
    110. {
    111. int flags = Qt::AlignHCenter | Qt::AlignTop;
    112. boundString = painter -> boundingRect(boundString, flags, s_label);
    113. painter -> drawText(x - (boundString.width() + 5), rect.bottom() - (boundString.height() + 5),
    114. boundString.width(), boundString.height(), flags, s_label);
    115. }
    116. }
    117. if(mode != NULL)
    118. {
    119. int flags = Qt::AlignCenter;
    120. boundString = painter -> boundingRect(boundString, flags, mode -> x_param.param_name);
    121. painter -> drawText(rect.right() - boundString.width(), rect.bottom() - 2 * boundString.height(),
    122. mode -> x_param.param_name);
    123. }
    124.  
    125. /// Отрисовка осей вдоль оси Y
    126. for(int j=0; j<=settings.numYTicks; ++j)
    127. {
    128. int y = rect.bottom() - (j * (rect.height() - 1) / settings.numYTicks);
    129. double label = settings.minY + (j * settings.spanY() / settings.numYTicks);
    130. QString s_label = GraphicDisplay::round(label);
    131.  
    132. painter -> setPen(Qt::black);
    133. painter -> drawLine(rect.left(), y, rect.right(), y);
    134. if(j != settings.numYTicks)
    135. {
    136. int flags = Qt::AlignRight | Qt::AlignTop;
    137. boundString = painter -> boundingRect(boundString, flags, s_label);
    138. painter -> drawText(rect.left() + 7, y - boundString.height(),
    139. boundString.width(), boundString.height(), flags, s_label);
    140. }
    141. }
    142. if(mode != NULL)
    143. {
    144. int flags = Qt::AlignCenter;
    145. boundString = painter -> boundingRect(boundString, flags, mode -> y_param.param_name);
    146. painter -> drawText(rect.left() + 7, rect.top() + boundString.height(),
    147. mode -> y_param.param_name);
    148. }
    149. painter -> drawRect(rect.adjusted(0, 0, -1, -1));
    150. }
    151.  
    152. /// Mode border drawing
    153. void QModeGraphics::drawModeBorder(QPainter* painter)
    154. {
    155. QRect rect(modesDisplay -> rect());
    156. if(!rect.isValid()) return;
    157. if(mode == NULL) return;
    158.  
    159. for(mode_border_iter iter=mode -> mode_border.begin(); iter!=mode -> mode_border.end(); ++iter)
    160. {
    161. QColor color = iter -> border_color;
    162. QPen pen(Qt::black);
    163. QBrush brush(Qt::DiagCrossPattern);
    164. brush.setColor(color);
    165. QPolygonF polyline(iter -> border_pnt.size());
    166. int j = 0;
    167. for(pntIter jter=iter -> border_pnt.begin(); jter!=iter -> border_pnt.end(); ++jter, ++j)
    168. {
    169. double dx = (settings.minX < 0) ? jter -> x() - settings.minX : jter -> x() - settings.maxX;
    170. double dy = jter -> y() - settings.minY;
    171. double x = (settings.minX < 0) ? rect.right() - (dx * (rect.width() - 1) / settings.spanX()) :
    172. rect.right() + (dx * (rect.width() - 1) / settings.spanX());
    173. double y = rect.bottom() - (dy * (rect.height() - 1) / settings.spanY());
    174. polyline[j] = QPointF(x, y);
    175. }
    176. painter -> setPen(pen);
    177. painter -> setBrush(brush);
    178. painter -> drawPolygon(polyline);
    179. }
    180. }
    181.  
    182. /// Инициализация кривых нужными значениями
    183. void QModeGraphics::initCurve()
    184. {
    185. if(mode == NULL) return;
    186. QRect rect(modesDisplay -> rect());
    187.  
    188. for(QVector<PARAMVALUE>::iterator iter=mode -> x_param.log_param.begin(); iter!=mode -> x_param.log_param.end();
    189. ++iter)
    190. {
    191. double dx = (settings.minX < 0) ? iter -> value - settings.minX : iter -> value - settings.maxX;
    192. double x = (settings.minX < 0) ? rect.right() - (dx * (rect.width() - 1) / settings.spanX()) :
    193. rect.right() + (dx * (rect.width() - 1) / settings.spanX());
    194. QPointF pnt(x, 0.0);
    195. curve_vec.push_back(pnt);
    196. }
    197.  
    198. for(int i=0; i<mode -> y_param.log_param.size(); ++i)
    199. {
    200. QVector<PARAMVALUE>::iterator iter = &mode -> y_param.log_param[i];
    201. double dy = iter -> value - settings.minY;
    202. double y = rect.bottom() - (dy * (rect.height() - 1) / settings.spanY());
    203. curve_vec[i].setY(y);
    204. }
    205. }
    206.  
    207. /// Draw curve
    208. void QModeGraphics::drawCurves(QPainter* painter)
    209. {
    210. if(mode == NULL) return;
    211. if(mode -> x_param.param_record == NULL || mode -> y_param.param_record == NULL)
    212. return;
    213.  
    214. painter -> setPen(Qt::blue);
    215. QPolygonF polyline(curve_vec.size());
    216. for(int i=0; i<curve_vec.size(); ++i)
    217. polyline[i] = curve_vec[i];
    218. painter -> drawPolyline(polyline);
    219. }
    220.  
    221. /// scaling +
    222. void QModeGraphics::zoomIn(int delta)
    223. {
    224. if(delta > 0) settings.scroll(-1 * delta, -1 * delta, delta, delta);
    225. else settings.scroll(delta, delta, -1 * delta, -1 * delta);
    226. update();
    227. }
    228.  
    229. /// scaling -
    230. void QModeGraphics::zoomOut(int delta)
    231. {
    232. if(delta > 0) settings.scroll(delta, delta, -1 * delta, -1 * delta);
    233. else settings.scroll(-1 * delta, -1 * delta, delta, delta);
    234. update();
    235. }
    To copy to clipboard, switch view to plain text mode 

    Where and when I should be to use your help code? What I should be to do that the graphic not stretch?

  14. #14
    Join Date
    Jun 2008
    Location
    Saint-Petersburg
    Posts
    50
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Scaling

    This code describing class PlotSettings!
    Qt Code:
    1. class PlotSettings
    2. {
    3. public:
    4. double minX;
    5. double maxX;
    6. int numXTicks;
    7. double minY;
    8. double maxY;
    9. int numYTicks;
    10.  
    11. protected:
    12. static void adjustAxis(double& min, double& max, int& numTicks);
    13.  
    14. public:
    15. PlotSettings();
    16. void scroll(int dx, int dy);
    17. void scroll(double mn_dx, double mn_dy, double mx_dx, double mx_dy);
    18. void adjust();
    19. double spanX() const { return fabs(fabs(maxX) - fabs(minX)); }
    20. double spanY() const { return fabs(fabs(maxY) - fabs(minY)); }
    21. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. PlotSettings::PlotSettings(): minX(-999.0), minY(-999.0), maxX(-999.0), maxY(-999.0)
    2. {
    3. numXTicks = 8;
    4. numYTicks = 8;
    5. }
    6.  
    7. void PlotSettings::scroll(int dx, int dy)
    8. {
    9. double stepX = spanX() / numXTicks;
    10. minX += dx * stepX;
    11. maxX += dx * stepX;
    12.  
    13. double stepY = spanY() / numYTicks;
    14. minY += dy * stepY;
    15. maxY += dy * stepY;
    16. }
    17.  
    18. void PlotSettings::scroll(double mn_dx, double mn_dy, double mx_dx, double mx_dy)
    19. {
    20. double stepX = spanX() / numXTicks;
    21. minX += mn_dx * stepX;
    22. maxX += mx_dx * stepX;
    23.  
    24. double stepY = spanY() / numYTicks;
    25. minY += mn_dy * stepY;
    26. maxY += mx_dy * stepY;
    27. }
    28.  
    29. void PlotSettings::adjust()
    30. {
    31. adjustAxis(minX, maxX, numXTicks);
    32. adjustAxis(minY, maxY, numYTicks);
    33. }
    34.  
    35. void PlotSettings::adjustAxis(double& min, double& max, int& numTicks)
    36. {
    37. const int MinTicks = 5;
    38. double grossStep = (max - min) / MinTicks;
    39. double step = pow(10.0, floor(log10(grossStep)));
    40.  
    41. if(5 * step < grossStep)
    42. step *= 5;
    43. else if(2 * step < grossStep)
    44. step *= 2;
    45.  
    46. numTicks = int(ceil(max / step) - floor(min / step));
    47. if(numTicks < MinTicks)
    48. numTicks = MinTicks;
    49.  
    50. min = floor(min / step) * step;
    51. max = ceil(max / step) * step;
    52. }
    To copy to clipboard, switch view to plain text mode 

  15. #15
    Join Date
    Jun 2008
    Location
    Saint-Petersburg
    Posts
    50
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Scaling

    Help, please!

  16. #16
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Scaling

    Patience, please. I got other things to do, too...


    Well, where to put it depends on what is wrong.
    Basically, you can wrap the drawing of your stuff with the code.
    (If only the grid is too small, wrap the drawGrid stuff, if all is too small, put it into paintEvent().

    so, try

    Qt Code:
    1. void QModeGraphics::paintEvent(QPaintEvent* events)
    2. {
    3. painter.begin(this);
    4. painter.setWindow(modesDisplay -> rect());
    5. painter.setFont(QFont("Tahoma", 8, Qt::SolidLine));
    6. painter.setPen(Qt::black);
    7.  
    8. initCurve();
    9.  
    10. painter.save();
    11. QPoint offset=...;
    12. painter.translate(offset),
    13.  
    14. drawGrid(&painter);
    15. drawCurves(&painter);
    16. drawModeBorder(&painter);
    17.  
    18. painter.end();
    19.  
    20. painter.restore();
    21.  
    22. QDialog::paintEvent(events);
    23. }
    To copy to clipboard, switch view to plain text mode 

    If your code is working, and you are just not happy about the graphics you produce, how about giving us a screenshot and describing what looks you would like to achieve.

    PS: The code is helpful; I'd just like to point out that we still can not try it out. We lack the .ui-file
    PPS: The use of fonts like "Tahoma" is dangerous. Your target platform might not have it. So the looks of the program might be diffrent.

  17. #17
    Join Date
    Jun 2008
    Location
    Saint-Petersburg
    Posts
    50
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Scaling

    This problem is resolved! )))) URAAAAA
    Qt Code:
    1. class PlotSettings
    2. {
    3. public:
    4. double minX;
    5. double maxX;
    6. int numXTicks;
    7. double minY;
    8. double maxY;
    9. int numYTicks;
    10.  
    11. protected:
    12. static void adjustAxis(double& min, double& max, int& numTicks);
    13.  
    14. public:
    15. PlotSettings();
    16. void scroll(int dx, int dy);
    17. void scale(double delta_x, double delta_y);
    18. void adjust();
    19. double spanX() const { return fabs(maxX - minX); }
    20. double spanY() const { return fabs(maxY - minY); }
    21. };
    22.  
    23.  
    24. /// Увеличение/уменьшение значения minX, maxX, minY, maxY на интервал между 2-мя отметками
    25. void PlotSettings::scale(double delta_x, double delta_y)
    26. {
    27. if((minX == maxX || minY == maxY) && delta_x < 0 && delta_y < 0) return;
    28.  
    29. double stepX = spanX() / numXTicks;
    30. minX -= delta_x * stepX;
    31. maxX += delta_x * stepX;
    32.  
    33. double stepY = spanY() / numYTicks;
    34. minY -= delta_y * stepY;
    35. maxY += delta_y * stepY;
    36. }
    37.  
    38.  
    39.  
    40. /// Класс для отображения параметров по времени или по расстоянию
    41. class GraphicDisplay : public QDialog, public Ui::GraphicDisplayClass
    42. {
    43. Q_OBJECT
    44.  
    45. private:
    46. QMap<int, QVector<QPointF>> curveMap; ///< список всех изображаемых кривых
    47. ParamPlotSettings settings; ///< настройка для определения масштаба
    48. QRubberBand* rubber; ///< "резиновая лента"
    49. QPoint origin; ///< начальные координаты выделяемой области
    50. bool rubberBandIsShown; ///< флажок попадания курсора в "резиновую ленту"
    51. QPen myPen; ///< карандаш для рисования линий определенной жирности и цвета
    52. enum { MARGIN = 10 };
    53.  
    54. QPainter painter; ///< рисовальщик
    55. QVector<QPointF> data; ///< вектор загружаемой кривой
    56.  
    57. private:
    58. void drawGrid(QPainter* painter);
    59. void drawCurves(QPainter* painter);
    60. void drawLegend(QPainter* painter, QRect& rect, int& k, int id);
    61. QPointF initXY(double& sx, double& sy);
    62.  
    63. protected:
    64. void paintEvent(QPaintEvent* events);
    65. void keyPressEvent(QKeyEvent* events);
    66. void wheelEvent(QWheelEvent* events);
    67. void mousePressEvent(QMouseEvent* events);
    68. void mouseMoveEvent(QMouseEvent* events);
    69. void mouseReleaseEvent(QMouseEvent* events);
    70. void resizeEvent(QResizeEvent* events) { QDialog::resizeEvent(events); update(); }
    71. void closeEvent(QCloseEvent* events) { QDialog::closeEvent(events); }
    72. void showEvent(QShowEvent* events) { QDialog::showEvent(events); }
    73.  
    74. public:
    75. GraphicDisplay(QWidget *parent = 0);
    76. GraphicDisplay(QWidget *parent, ParamPlotSettings& st);
    77. ~GraphicDisplay();
    78. void zoom(double delta) { settings.scale(delta, delta); settings.adjust(); update(); }
    79. void setPlotSettings(const ParamPlotSettings& sts) { settings = sts; settings.adjust(); update(); }
    80.  
    81.  
    82. /// Изменение масштаба при движении колесика
    83. void GraphicDisplay::wheelEvent(QWheelEvent* events)
    84. {
    85. int numDegrees = events -> delta() / 8;
    86. double numTicks = numDegrees / 15.0;
    87.  
    88. zoom(numTicks);
    89. update();
    90. }
    91.  
    92. /// Нажатие на кнопки клавиатуры
    93. void GraphicDisplay::keyPressEvent(QKeyEvent* events)
    94. {
    95. switch(events -> key())
    96. {
    97. case Qt::Key_Plus:
    98. zoom(1.0);
    99. break;
    100. case Qt::Key_Minus:
    101. zoom(-1.0);
    102. break;
    103. case Qt::Key_Left:
    104. settings.scroll(-1, 0);
    105. update();
    106. break;
    107. case Qt::Key_Right:
    108. settings.scroll(1, 0);
    109. update();
    110. break;
    111. case Qt::Key_Up:
    112. settings.scroll(0, 1);
    113. update();
    114. break;
    115. case Qt::Key_Down:
    116. settings.scroll(0, -1);
    117. update();
    118. break;
    119. default:
    120. QWidget::keyPressEvent(events);
    121. }
    122. }
    123.  
    124. /// Отрисовка сетки
    125. void GraphicDisplay::drawGrid(QPainter* painter)
    126. {
    127. QRect rect(paramsDisplay -> rect());
    128. if(!rect.isValid()) return;
    129.  
    130. QRect boundString;
    131. int _max = max(settings.numXTicks, settings.numYTicks);
    132. for(int i=0, j=0, k=0; i<=_max; ++i, ++j, ++k)
    133. {
    134. if(j <= settings.numXTicks) ///< отрисовка по оси X
    135. {
    136. int x = rect.left() + (j * (rect.width() - 1) / settings.numXTicks);
    137. double label = settings.minX + (j * settings.spanX() / settings.numXTicks);
    138. QString s_label = round(label);
    139.  
    140. painter -> setPen(Qt::black);
    141. painter -> drawLine(x, rect.top(), x, rect.bottom());
    142. if(j != settings.numXTicks)
    143. {
    144. int flags = Qt::AlignHCenter | Qt::AlignTop;
    145. boundString = painter -> boundingRect(boundString, flags, s_label);
    146. painter -> drawText(x - (boundString.width() + 5), rect.bottom() - (boundString.height() + 5),
    147. boundString.width(), boundString.height(), flags, s_label);
    148. }
    149. }
    150. if(k <= settings.numYTicks) ///< отрисовка по оси Y
    151. {
    152. int y = rect.bottom() - (k * (rect.height() - 1) / settings.numYTicks);
    153. double label = settings.minY + (k * settings.spanY() / settings.numYTicks);
    154. QString s_label = round(label);
    155.  
    156. painter -> setPen(Qt::black);
    157. painter -> drawLine(rect.left(), y, rect.right(), y);
    158. if(k != settings.numYTicks)
    159. {
    160. int flags = Qt::AlignRight | Qt::AlignTop;
    161. boundString = painter -> boundingRect(boundString, flags, s_label);
    162. painter -> drawText(rect.left() + 5, y - boundString.height(),
    163. boundString.width(), boundString.height(), flags, s_label);
    164. }
    165. }
    166. }
    167. painter -> drawRect(rect.adjusted(0, 0, -1, -1));
    168. }
    169.  
    170. /// Отрисовка легенды
    171. void GraphicDisplay::drawLegend(QPainter* painter, QRect& rect, int& k, int id)
    172. {
    173. QString pname = fact_prm[id].param_name;
    174. QString dimension = fact_prm[id].dimension;
    175. myPen.setWidth(1);
    176. painter -> drawLine(QLineF(QPointF(rect.right() - 45, rect.top() + 25 * k),
    177. QPointF(rect.right() - 65, rect.top() + 25 * k)));
    178. painter -> setPen(Qt::black);
    179. int flags = Qt::AlignCenter;
    180. QRect boundString = painter -> boundingRect(boundString, flags, pname);
    181. painter -> drawText(QPointF(rect.right() - (boundString.width() + 7), rect.top() + 25 * k), pname);
    182. boundString = painter -> boundingRect(boundString, flags, dimension);
    183. painter -> drawText(QPointF(rect.right() - (boundString.width() + 7),
    184. rect.bottom() - 2 * boundString.height()), dimension);
    185. ++k;
    186. }
    187.  
    188. /// Инициализация координат
    189. QPointF GraphicDisplay::initXY(double& sx, double& sy)
    190. {
    191. QRect rect(paramsDisplay -> rect());
    192. double dx, dy;
    193. dx = sx - settings.minX;
    194. dy = sy - settings.minY;
    195. double x = rect.left() + (dx * (rect.width() - 1) / settings.spanX());;
    196. double y = rect.bottom() - (dy * (rect.height() - 1) / settings.spanY());
    197.  
    198. return QPointF(x, y);
    199. }
    200.  
    201. /// Отрисовка графика
    202. void GraphicDisplay::drawCurves(QPainter* painter)
    203. {
    204. QRect rect(paramsDisplay -> rect());
    205. if(!rect.isValid()) return;
    206.  
    207. painter -> setClipRect(rect.adjusted(1, 1, -1, -1));
    208. QMapIterator<int, QVector<QPointF>> iter(curveMap);
    209. int k = 1;
    210. while(iter.hasNext())
    211. {
    212. iter.next();
    213.  
    214. int id = iter.key();
    215. TP_PARAM ty = fact_prm[id].type;
    216. if(settings.win_type != ty) continue;
    217.  
    218. const QVector<QPointF> &data = iter.value();
    219. QPolygonF polyline(data.count());
    220. for(int j=0; j<data.count(); ++j)
    221. {
    222. double x = data[j].x();
    223. double y = data[j].y();
    224. polyline[j] = initXY(x, y);
    225. }
    226. QString color = fact_prm[id].param_color.name();
    227. QPen oldPen = painter -> pen();
    228. myPen.setColor(fact_prm[id].param_color);
    229. painter -> setPen(myPen);
    230. painter -> drawPolyline(polyline);
    231. int width = myPen.width();
    232.  
    233. drawLegend(painter, rect, k, id);
    234.  
    235. painter -> setPen(oldPen);
    236. }
    237. }
    To copy to clipboard, switch view to plain text mode 

    This is describing the code which have resolved problem!

Similar Threads

  1. Scaling Painter without scaling the coordinate sytem
    By maverick_pol in forum Qt Programming
    Replies: 4
    Last Post: 7th January 2008, 21:30
  2. QPixmap scaling question
    By eric in forum Qt Programming
    Replies: 6
    Last Post: 20th November 2007, 17:40
  3. Ignoring scaling graphics view/scale
    By maverick_pol in forum Qt Programming
    Replies: 1
    Last Post: 7th August 2007, 17:44
  4. Pixmap scaling
    By yellowmat in forum Newbie
    Replies: 3
    Last Post: 4th January 2007, 16:01
  5. Replies: 12
    Last Post: 25th May 2006, 21:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.