Saturday, December 29, 2012

GUI – Edit Box (Begin)

To create the new EditBox class source and header file, the new file wizard of QtCreator was again used (New File or Project... under the File menu). Under File and Classes the C++ option was selected and then C++ Class was selected to create both files and Choose... was clicked.

On the next C++ Class Wizard dialog, the Class name: was set to EditBox, Base class: was set to QTextEdit and for Type information: the Inherits QWidget option was selected.  The rest were left to the filled in defaults.  On the final Project Management dialog, git was selected to add to version control and the wizard was finished.

For now, nothing was changed in the created editbox.h and editbox.cpp files except the customary comment headers were added and the formatting was corrected to match the style of the rest of the project files.  Functionality for the EditBox class will be implemented in the commits that follow.  The sources files were also added to the CMake build file, and because the EditBox class contains QOBJECT, the editbox.h file was added to the MOC sources instead of the header files variable.

A new EditBox member pointer was added to the MainWindow class.  In the MainWindow constructor, an EditBox instance was created.  There is no need to give it a parent here because the next call to setCentralWidget() does this automatically, and makes the edit box widget the main widget of the main window.  This also means that for new there is no need to delete the EditBox instance because Qt will do this when the MainWindow instance is deleted upon the program closing.

Eventually this basic mechanism of how the single EditBox instance is maintained will change since there will be a need to have multiple edit boxes open (the plan is that subroutines and functions will be contained in their own edit boxes).  The exact interface hasn't been designed or decided yet (split windows, tabs, multiple windows, etc. are all possibilities).

[commit de0ec4eb2b]

C++ Explicit Constructors

The new EditBox class was created using QtCreator's new class wizard and I noticed that it put the explicit keyword in front of the constructor definition (it also did this with the MainWindow class).  (More about the new EditBox class in the post.)  Curious what this keyword does (which was added since I learned C++ in the early 90's), I did some research.  The explicit keyword tells the compiler not to allow implicit type conversions on constructors that take a single argument.  Consider this partial example:
class MyFloat {
    float m_value;
public:
    MyFloat(int value): m_value(value) {}
    float value(void) { return m_value; }
};

float square(MyFloat myFloat) {
    return myFloat.value() * myFloat.value();
}

MyFloat something = 47;
std::cout << square(12) << std::endl;
On the last two lines, implicit conversions occur from int to MyFloat, which are perfectly acceptable  for this simple class.  However, if the implicit conversion was not desired, the explicit keyword can be added in front of the constructor to prevent it.  The compiler will now generate errors for the last two lines above.  These lines can be rewritten to work:
MyFloat something = MyFloat(47);
std::cout << square(MyFloat(12)) << std::endl;
This is a contrived example and would probably make sense to allow the implicit conversion in this class, however, this is not the case with the classes used in this project.  Therefore, the explicit keyword was added to all the constructors that take a single argument, which included the CommandLine, Parser, Tester, Token and Translator classes.

The most glaring constructor requiring explicit was the Token constructor Token(int column = -1) that would have allowed a statement like Token token = 47;, which does not make sense (a token should not be assigned an integer).

[commit 5979939788]