Hi all,

I'm going to open a new thread as I guess other doesn't get attention (maybe 'cause i didn't open a new more specific thread, and other users didn't scroll at the end). I hope to do stuff in right way (about forum section, how to "up" a post, and so on...) if it's not the case sorry in advance.

This is my question, but I'm going to re-write it here so it's more handy.

I tried to drow my calendar in a QWidget as Santosh suggested. But some stuff are hard to do.
When I zoom in scrollbar can't let me see the whole calendar. Maybe it's a QScrollArea issue.
I read this post for widgets nesting. Adding a further QWidget I got QScrollBars visible. So actually my widgets nesting is this(1):

Qt Code:
  1. QGroupBox (with vertical layout)
  2. |
  3. |-some stuff for header bar (with zoom in/out buttons, and so on)
  4. |-QScrollArea (with vertical layout, widgetResizable property set to "true")
  5. |
  6. |-QWidget (as used into above post)
  7. |
  8. |-QWidget promoted to my DailyWidget class
To copy to clipboard, switch view to plain text mode 

I have part of the code into an Abstract class as I plan to create several calendar's views (daily,weekly,and so on..)
Therefore zoom in/out methods are into this abstract class(2):

Qt Code:
  1. //zoom in and out:
  2. void AbstractWidget::slotZoom(int delta)
  3. {
  4. qreal scaleTemp = m_scaleFactor;
  5. if(delta>0)
  6. {
  7. scaleTemp *= 1.25;
  8. }else{
  9. scaleTemp *= 0.8;
  10. }
  11. if(scaleTemp>=1.0 && scaleTemp<3.0)
  12. {
  13. m_scaleFactor = scaleTemp;
  14.  
  15. if(!m_scrollarea)
  16. getScrollArea();
  17. if(m_scrollarea)
  18. {
  19. QScrollBar *sb = m_scrollarea->horizontalScrollBar();
  20.  
  21. adjustScrollBar(sb, scaleTemp);
  22.  
  23. sb = m_scrollarea->verticalScrollBar();
  24.  
  25. adjustScrollBar(sb, scaleTemp);
  26. }
  27. update();
  28. updateGeometry();
  29. }
  30. }
  31.  
  32. void AbstractWidget::adjustScrollBar(QScrollBar *scrollBar, double factor)
  33. {
  34. if(scrollBar)
  35. {
  36. scrollBar->setValue(int(factor * scrollBar->value() + ((factor - 1) * scrollBar->pageStep()/2)));
  37. }
  38. }
To copy to clipboard, switch view to plain text mode 

getScrollArea method is a bit ugly and it rely on the widgets nesting to get scrollarea(3):

Qt Code:
  1. void AbstractWidget::getScrollArea()
  2. {
  3. m_scrollarea = (QScrollArea *)this->parent()->parent()->parent()->parent();
  4. }
To copy to clipboard, switch view to plain text mode 

and finally this is the painting code of my DailyWidget(4):

Qt Code:
  1. DailyWidget::DailyWidget(QWidget *parent)
  2. : AbstractWidget(parent),
  3. m_timer(new QTimer(this))
  4. {
  5. m_view = AbstractWidget::DailyView;
  6. m_scaleFactor = 1.0;
  7. m_fontFactor = 12;
  8. setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
  9. connect(m_timer, SIGNAL(timeout()), this, SLOT(slotUpdateHourHighlightLine()));
  10. }
  11.  
  12. void DailyWidget::paintEvent(QPaintEvent *event)
  13. {
  14. QWidget::paintEvent(event);
  15. QPainter m_painter(this);
  16. m_painter.scale(m_scaleFactor,m_scaleFactor);
  17. m_painter.setRenderHints(QPainter::Antialiasing|QPainter::TextAntialiasing|QPainter::HighQualityAntialiasing,true);
  18. QSize s(997*m_scaleFactor,580*m_scaleFactor);
  19. resize(s);
  20. drawGrid(m_painter);
  21. }
  22.  
  23. void DailyWidget::drawGrid(QPainter &painter)
  24. {
  25. const int linesNumber = 48;
  26. QSize msize = size();
  27. int width = msize.width();
  28. int counter = 0;
  29. qreal leftLine = 0.0;
  30. qreal y = 0;//initialized with paddingtop
  31. qreal gridSize = msize.height()/(qreal)linesNumber;
  32. QTime time;
  33. QFont textFont("Times New Roman",gridSize/2);
  34. painter.setBrush(whiteBrush);
  35. painter.setPen(blackPen);
  36. painter.drawRect(0,0,width,msize.height());
  37.  
  38. painter.setFont(textFont);
  39. m_eventRect = QRectF();
  40. painter.setBrush(whiteBrush);
  41. time.setHMS(counter+1,0,0);
  42. QRectF textRect = painter.boundingRect(QRect(0,0,0,0),time.toString("hh:mm"));
  43. leftLine = textRect.width()+10;
  44. painter.setBrush(whiteBrush);
  45. for(int i=counter;i<linesNumber;i++)
  46. {
  47. if(i%2==0)
  48. {//hours:
  49. painter.setPen(hourPen);
  50. painter.drawLine(0,y,width,y);
  51.  
  52. time.setHMS(counter++,0,0);
  53. painter.setPen(blackPen);
  54. painter.drawText(5,y+textRect.height(),time.toString("hh:mm"));
  55. }else{//half hour:
  56. painter.setPen(halfHourPen);
  57. painter.drawLine(leftLine+10,y,width,y);
  58. }
  59. y += gridSize;
  60. }
  61. painter.setPen(hourPen);
  62. painter.drawLine(0,y,width,y);
  63.  
  64. painter.setPen(redPen);
  65. painter.drawLine(leftLine+10,0,leftLine+10,y);
  66. m_eventRect.setX(0.0);
  67. m_eventRect.setY(0.0);
  68. m_eventRect.setWidth(width);
  69. m_eventRect.setHeight(y);
  70. QSize AdjustSize = this->size();
  71. setMinimumSize(AdjustSize);
  72. }
To copy to clipboard, switch view to plain text mode 

As explained above when I zoom in drawing is zoomed as well, but QScrollArea can't let user reach the end of chart.
I'm going to attach two images, respectively zoom out and zoom in.

Last note: I tried to do several test i.e. added and removed row 18 at (4), but the point is that I don't have any clue about where to look at.

Thanks all for any advice.
Alberto

zoomout.jpgzoomin.jpg