subcommanderblog.wordpress.com

Subcommander Development Blog

Archive for January 2009

Improving submerge wheel scrolling

leave a comment »

submerge (subcommanders diff and merge tool) uses the mouse wheel to quickly jump from one difference to the next or the previous difference. Let’s call this jump scrolling.

To achieve this I handle the mouse wheel event in the parent widget of the of the widget that really displays the files. To do normal scrolling, i.e. line by line, one has to move the mouse above the scroll bar which handles the mouse wheel in the usual way.

Sometimes this is a bit annoying if you just like to scroll a few lines up or down to review the visible difference. You move the wheel and … ups, you are at another difference in the file, maybe several pages from the difference you just reviewed. So the idea is to allow normal scrolling by using a keyboard modifier. Maybe I will even switch the default, and change jump scrolling to shift + wheel.

I’m currently reading “Clean Code” from Robert C. Martin, although it doesn’t offer new revolutionary ideas, it is always good to be remembered of things you already know but don’t follow consistently.  So let’s see if I can also improve the readability of the code, at least a bit :-).

Here is the code as it was before. A few notes about the code:

  • Diff3Widget is the parent widget that shows all versions of the file we diff  (old file, new file etc.).
  • _diffInfo is a member that holds the actual diff information.
  • the diff information contains several block. A block represents a number of equal lines in the two file or a number lines that are different in both files. That’s the reason why diff number and block number are not the same. Hmm, not sure if this is clear ;-)

 

void Diff3Widget::wheelEvent( QWheelEvent* e )
{
  if( ! _diffInfo )
  {
    e->ignore();
    return;
  }

  //printf( "wheel delta: %d (%p)\n", e->delta(), e );

  int b = 0;

  if( e->delta() > 0 )
  {
    // forward, down
    if( _diffInfo->hasPrevDiff() )
    {
      b = _diffInfo->prevDiff();
    }
  }
  else
  {
    // backward, up
    if( _diffInfo->hasNextDiff() )
    {
      b = _diffInfo->nextDiff();
    }
  }

  if( b == 0 )
  {
    e->ignore();
    return;
  }

  jumpToBlock( b );
  setActiveDiff( _diffInfo->getActiveDiff() );

  emit diffChanged( _diffInfo->getActiveDiff() );

  e->accept();
}

 

I ended up with the code below. It uses two members that display the files in submerge (_single is the original file (the third file) if any and _double which contains the left and right file) to do the normal scrolling. I also dropped the _diffInfo pointer check. I had already a null implementation for the diff info and I now initialize the member with it so I do not have to check for a NULL pointer.

 

void Diff3Widget::wheelEvent( QWheelEvent* e )
{
  if( shouldScrollNormal(e->modifiers()) )
    scrollNormal(e->delta());
  else
    scrollToDiff(e->delta());

  e->accept();
}

void Diff3Widget::scrollNormal( int delta )
{
  QScrollBar* sbSingle = _single->getVScrollBar();
  QScrollBar* sbDouble = _double->getVScrollBar();
  sbSingle->setValue(sbSingle->value()-delta);
  sbDouble->setValue(sbDouble->value()-delta);
}

bool Diff3Widget::shouldScrollNormal( Qt::KeyboardModifiers modifiers )
{
  if( modifiers == Qt::ShiftModifier )
    return true;

  if( _diffInfo->getDiffCnt() == 0 )
    return true;

  return false;
}

void Diff3Widget::scrollToDiff( int delta )
{
  if( delta > 0 )
    scrollToPrevDiff();
  else
    scrollToNextDiff();
}

void Diff3Widget::scrollToPrevDiff()
{
  if( !_diffInfo->hasPrevDiff() )
    return;

  jumpToBlock( _diffInfo->prevDiff() );
  setActiveDiff( _diffInfo->getActiveDiff() );
}

void Diff3Widget::scrollToNextDiff()
{
  if( !_diffInfo->hasNextDiff() )
    return;

  jumpToBlock( _diffInfo->nextDiff() );
  setActiveDiff( _diffInfo->getActiveDiff() );
}

 

I split up the original method into several methods and reduced the code in each method to what is interesting at that level. Clearly better than before but I think there are still a few things you can suggest to improve the code. Anything?

What’s missing is some test code… how would you test this?

Written by hauner

Saturday, 24 January, 2009 at 19:17

Posted in submerge

Subcommander FAQ

leave a comment »

Someone was searching the web for ‘subcommander’ and ‘proxy’ and came here. Looks like it is not obvious how to run subcommander using a http proxy. I guess that is right :(

So I started a Subcommander FAQ using the new wiki feature on tigris.org. The faq is here: http://subcommander.tigris.org/wiki/faq and I added a first question and of course an answer  ;-)

Written by hauner

Saturday, 17 January, 2009 at 18:37

Posted in subcommander, submerge

Tagged with

CPPUNIT_ASSERT_EQUAL and custom data types

with 2 comments

To write more bookmark specific settings information to the configuration file I rewrote and simplified the code that reads subcommanders project settings.  I added a few tests for the new refactored code and  used a ccpunit feature  I had discovered a while ago: assertion traits.

If you have never heard of it read on and read what an assertion trait is and how it can help in handling custom data types in cppunit tests.

I’m using a custom build string class in the subversion related code to handle utf-8 encoded strings (sc::String) in Subcommander.

This works well, but there was an issue when trying to use Strings in test code:

CPPUNIT_ASSERT_EQUAL( expected, actual );

The assertion above “crashed” when printing its error message when one of the two strings was an empty string. It crashed in stl output stream code that was called by the following code inside cppunit (from TestAssert.h):

template <class T> struct assertion_traits
{
    static bool equal( const T& x, const T& y )
    {
        return x == y;
    }

    static std::string toString( const T& x )
    {
        OStringStream ost;
        ost << x;
        return ost.str();
    }
};

Very interesting code!  CPPUNIT_ASSERT_EQUAL(a,b) calls the equal method in this template to compare its two parameters a and b and calls the toString method to print the failure message if the assertion fails.

By using a template feature that’s called template specialization we can re-implement the template for a specific data type. If the compiler finds a specialization for a specific type, in my case my custom String class, it  uses the specialization for this type instead of  instantiating the generic T template for the type.

Here is the “new” code for my String class. It is nearly the same as the generic code but handles the empty string case in toString (there is also an example in cppunit’s  TestAssert.h file):

template<> struct CPPUNIT_NS::assertion_traits<sc::String>
{
  static bool equal( const sc::String& x, const sc::String& y )
  {
    return x == y;
  }

  static std::string toString( const sc::String& x )
  {
    std::string text = '"' + (!x.isEmpty() ? std::string(x) : "<null>") + '"';
    OStringStream ost;
    ost << text;
    return ost.str();
  }
};

The interesting thing about specializing the generic assertion traits is that you can add support for any custom data type to CPPUNIT_ASSERT_EQUAL with just a few lines of code.

This is even a documented feature. Surprisingly a google search on assertion_traits delivers only about 220 hits.

Either nobody knows about this feature or it just so obvious that nobody cares to write about it :-)

Written by hauner

Saturday, 10 January, 2009 at 18:14

Posted in subcommander

Tagged with ,

Linux .desktop files and the Subcommander SVG Logo

leave a comment »

While adding Linux .desktop files for subcommander and submerge I got a bit sidetracked and tried to convert the Subcommander logo to an svg file. The original file is a psp vector image file (from an older PaintShopPro version). At first I tried to find a file format that would let me export it and import it again into Inkscape (on a Mac). Shouldn’t be to difficult.

No way! Both programs understand a number of different formats but nothing that really worked. Because I did not want to spend my day trying different image converters I simply started to re-create the logo in Inkscape. Its just two bezier curves. After a while of fiddeling around with Inkscape (I have never done anything usfull with Inkscape before) I finally created something that is not 100% the same but at least 98% ;-)

How do you like the white logo? (a 265×265 png exported from Inkscape)

logowhite

White Subcommander Logo

Back to the .desktop files. I don’t run Linux so anyone has an idea where to install them or the icons? Google came up with something like the list below but I’m not sure if this is universal for all Linux distributions or if it the same for KDE and Gnome.

  • /usr/share/applications/subcommander.desktop
  • /usr/share/applications/submerge.desktop
  • /usr/share/icons/???

So if you know, let me know :)

Written by hauner

Thursday, 1 January, 2009 at 19:11

Posted in subcommander, submerge