Results 1 to 19 of 19

Thread: timeline

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default timeline

    Hi to all, I'm developing an audio editor that at the moment has basic functionalities as draw the sound waveform, play the sound.
    I would implement a time line ( a moving vertical line ) that give to the user an idea of which part of the sound is playing.
    In the paintEvent I have to draw the waveform ( cached in a QImage ) and the time line.

    This is my paintEvent
    Qt Code:
    1. void WaveWidget::paintEvent( QPaintEvent* pe )
    2. {
    3. // Update the ev->rect area with the wave from the rendered QPixmap here
    4.  
    5. if( m_waveCachePixmap.isNull() )
    6. {
    7. updateWave();
    8. }
    9.  
    10. QPainter p( this );
    11. QPen pen = QPen(Qt::blue, 1);
    12. p.setRenderHint( QPainter::Antialiasing, true );
    13. p.drawImage( 0, 0, m_waveCachePixmap );
    14.  
    15. QRect& rect = QRect(pe->rect());
    16. p.setPen(pen);
    17.  
    18. p.drawLine( rect.x(), 0, rect.x(), height() );
    19. }
    To copy to clipboard, switch view to plain text mode 

    I also have to delete the previous line before to draw the new? Or the final result is a growing rectangle. How can I do?

    Best,
    Franco
    Franco Amato

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: timeline

    What exactly is the problem?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: timeline

    Quote Originally Posted by wysota View Post
    What exactly is the problem?
    Instead of drawing a horizzontally moving line it draw a rectangle that grow.
    Franco Amato

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: timeline

    The rect() you get in the paint event can be an arbitrary rectangle from within your widget's coordinates. I don't think you want to paint arbitrary rectangles on your widget...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: timeline

    Quote Originally Posted by wysota View Post
    The rect() you get in the paint event can be an arbitrary rectangle from within your widget's coordinates. I don't think you want to paint arbitrary rectangles on your widget...
    Sorry but I didn't get you.
    So what must I do? Where my code is wrong?
    Franco Amato

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: timeline

    Quote Originally Posted by franco.amato View Post
    So what must I do?
    Draw a line in coordinates you want it to be drawn at, not the ones passed to you by the event.

    Where my code is wrong?
    Here:
    Qt Code:
    1. QRect& rect = QRect(pe->rect());
    2. p.setPen(pen);
    3.  
    4. p.drawLine( rect.x(), 0, rect.x(), height() );
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: timeline

    Quote Originally Posted by wysota View Post
    Draw a line in coordinates you want it to be drawn at, not the ones passed to you by the event.



    Here:
    Qt Code:
    1. QRect& rect = QRect(pe->rect());
    2. p.setPen(pen);
    3.  
    4. p.drawLine( rect.x(), 0, rect.x(), height() );
    To copy to clipboard, switch view to plain text mode 
    Sorry but I don't understand you.
    How can I draw I line where I want whitout considering the rect()? How can I pass the coordinates of the line to the paintEvent?

    Can you explain to me?

    Best
    Franco Amato

  8. #8
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: timeline

    I solved it.
    The problem was the rect I passed to the paintEvent. It must completely contain the 2 lines ( old and new ). Something like this
    Qt Code:
    1. QRect r = QRect( old_line - 1, 0, new_line + 1, height() )
    To copy to clipboard, switch view to plain text mode 
    and pass r to the paintevent
    Qt Code:
    1. update( r )
    To copy to clipboard, switch view to plain text mode 

    Regards,
    Franco
    Franco Amato

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: timeline

    No, that's wrong as well. Take a window from another application and start moving it around over your widget. You will see it's getting messy.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: timeline

    Quote Originally Posted by wysota View Post
    No, that's wrong as well. Take a window from another application and start moving it around over your widget. You will see it's getting messy.
    So please let me know how must I do.


    Thank you
    Franco Amato

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: timeline

    Quote Originally Posted by franco.amato View Post
    So please let me know how must I do.
    I already told you - draw the line in a place where you want it drawn, not in a random place. The line has some logic behind it, right? Are you able to tell exactly where the line should be drawn at a specified arbitrary moment based on the state of the application?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: timeline

    Quote Originally Posted by wysota View Post
    I already told you - draw the line in a place where you want it drawn, not in a random place. The line has some logic behind it, right? Are you able to tell exactly where the line should be drawn at a specified arbitrary moment based on the state of the application?
    Yes Is exactly what I did
    Franco Amato

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: timeline

    Quote Originally Posted by franco.amato View Post
    Yes Is exactly what I did
    You mean now or when? Because none of the code you pasted does what I said.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: timeline

    Quote Originally Posted by wysota View Post
    You mean now or when? Because none of the code you pasted does what I said.
    Here my last code:
    Qt Code:
    1. void WaveWidget::paintEvent( QPaintEvent* pe )
    2. {
    3. if( m_waveCachePixmap.isNull() )
    4. {
    5. updateWave();
    6. }
    7.  
    8. QPainter p( this );
    9. QPen pen = QPen(Qt::blue, 1);
    10. p.setRenderHint( QPainter::Antialiasing, true );
    11. //wave
    12. p.drawPixmap( 0, 0, m_waveCachePixmap );
    13.  
    14. //timeline
    15. p.setPen(pen);
    16. p.drawLine( m_CurrentTimePosition, 0, m_CurrentTimePosition, height() );
    17. }
    To copy to clipboard, switch view to plain text mode 

    and the setCurrentTimePosition (as in my last post):
    Qt Code:
    1. void WaveWidget::setCurrentTimePosition()
    2. {
    3. uint newTimeValue = getCurrentPos();
    4.  
    5. /* trasform time coord in widget coord */
    6. qreal samplesPerPixel = m_wave->getSamples() / width();
    7. qreal x_pos = newTimeValue / samplesPerPixel;
    8.  
    9. if( x_pos == m_CurrentTimePosition )
    10. return; //no change
    11.  
    12. QRect r = QRect( m_CurrentTimePosition - 1, 0, x_pos, height() );
    13. update( r );
    14.  
    15. /* update current pos */
    16. m_CurrentTimePosition = x_pos;
    17. }
    To copy to clipboard, switch view to plain text mode 

    Is what did you suggested to me?
    Franco Amato

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: timeline

    Yes, it's much better now. The only thing I could suggest is to move updateWave() to setCurrentTimePosition() method. Your paintEvent() would then execute a bit faster which might be beneficial if complexity of your application grows.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #16
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: timeline

    Quote Originally Posted by wysota View Post
    Yes, it's much better now. The only thing I could suggest is to move updateWave() to setCurrentTimePosition() method. Your paintEvent() would then execute a bit faster which might be beneficial if complexity of your application grows.
    Hi. Imagine that scenario:
    1) I load an audio file -> the waveform is generated.
    2) I resize the window WITHOUT PLAY THE FILE
    in this case I have to update the wave so I can not move the updatewave inside the setCurrentTimePosition

    Did you get me?

    Best
    Franco Amato

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: timeline

    Quote Originally Posted by franco.amato View Post
    2) I resize the window WITHOUT PLAY THE FILE
    in this case I have to update the wave so I can not move the updatewave inside the setCurrentTimePosition
    Call updateWave() from the resizeEvent() as well. It's still worth it to write another method to have the paint event not have to execute the if statement every time your widget is redrawn.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. #18
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: timeline

    Quote Originally Posted by wysota View Post
    Call updateWave() from the resizeEvent() as well. It's still worth it to write another method to have the paint event not have to execute the if statement every time your widget is redrawn.
    I moved the updateWave inside the resize event but the resizing process now seems slower.

    What do you think about?
    Franco Amato

  19. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: timeline

    During every resize your wave is being updated. It would be wise to update it only when the user stops resizing but then it might not be worth the effort. Maybe you should stick with generating the pixmap during paint event for now until you reach a point where it becomes a bottleneck.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Creating a graphic timeline
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 26th November 2012, 13:03

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.