Wednesday, November 19, 2014

My codez is frawress

Sup boys and girls. Tweety here. Well here's another look into Qt, I've discovered that I need a way to change the database schema. So looking for something that resembles a "rake db:migrate" from ruby on rails I came up with this function:


bool Database::migrate()
{
    bool seccess = true;
    QString failed;
    QFile migrateFile("D:/Qt/Tools/QtCreator/bin/sqliteTest/migrate_file.in");
    QFile doneMigrate("D:/Qt/Tools/QtCreator/bin/sqliteTest/migrate_file.out");
    if(!migrateFile.open(QIODevice::ReadWrite) || !doneMigrate.open(QIODevice::WriteOnly|QIODevice::Append))
        seccess = false;
    QTextStream in(&migrateFile), out(&doneMigrate);
    while(!in.atEnd())
    {
        bool new_table = true;
        QString table_name;
        QString new_columns;
        QString old_columns;
        QString line = in.readLine();

        QStringList list = line.split(";");
        table_name = list[0];
        new_columns = list[1];
        qDebug()<<table_name<<"\t"<<new_columns;
        QSqlRecord rec = db.record(table_name);
        if(!rec.isEmpty())
        {
            new_table = false;
            for(int i = 0; i < rec.count();i++)
            {
                if(i==0)
                    old_columns += rec.fieldName(i);
                else
                {
                    old_columns += ", ";
                    old_columns += rec.fieldName(i);
                }
            }
        }
        qDebug()<<old_columns;
        QSqlQuery qry(db);
        if(!new_table)
        {
            if(!qry.exec(QString("ALTER TABLE %1 RENAME TO TempOldTable").arg(table_name)))
                seccess = false;
            if(!qry.exec(QString("CREATE TABLE %1 (%2) ").arg(table_name, new_columns)))
                seccess = false;
            if(!qry.exec(QString("INSERT INTO user (%1) SELECT %1 FROM TempOldTable").arg(old_columns)))
                seccess = false;
            if(!qry.exec("DROP TABLE TempOldTable"))
                seccess = false;
        }
        else
        {
            if(!qry.exec(QString("CREATE TABLE %1 (%2) ").arg(table_name, new_columns)))
                seccess = false;
        }
        if(seccess)
            out << line << endl;
        else
            failed += line + "\n";
        rec = db.record(table_name);
        if(!rec.isEmpty())
        {
            for(int i = 0; i < rec.count();i++)
            {
                if(i==0)
                    old_columns = rec.fieldName(i);
                else
                {
                    old_columns += ", ";
                    old_columns += rec.fieldName(i);
                }
            }
        }
        qDebug()<<old_columns;
    }
    migrateFile.resize(0);
    in << failed;
    doneMigrate.close();
    migrateFile.close();

    return seccess;
}


Now this is for Qt, but the logic is sound should work for any SQLite3 database. So let's go over the code a bit. First this uses a semi colon separated variables file. I used the ";" because there is commas that I want to keep in the string. How this file looks is each line would be a new or alter to a database tables. The format is [table_name];[columns] with all the different columns separated with commas. So first we have a bool for success or not, it's set to true and if anything fails it's set to false. Next we load a couple files, and open them. Open the out file with truncate. Then make a pair of streams for each file. Then we get each line from the "in" file, the one with the table name and columns. Take that line and split it on the ";". Assign the variables table_name and new_columns with the strings in the line we split. Next we get the columns from the table in the database. If rec is empty we know that there is no table with that name so we create a new one with the sqlite3 commands "CREATE TABLE". Else we change the name of the old table, create a new table with the original name and the new columns, insert everything from the old database table into the new one, then drop the old table. 
    If all that was a success we output the original line to the out file as a record of what has been done. If it failed then we copy that line to a new string then copy it back to the original file, so we can do it over. Then close the files. Not to bad right? Let me know what you think in the comments and any improvements that could help me or anyone else that might view this blog. Thanks for reading. We'll see you next time, but until then... I'm outta here.

Friday, May 30, 2014

Qt custom widget inside a widget

A school project has had me learn Qt. Which I learned the pronunciation is (cute) from a Youtube tutorial. In this program we have a GUI that is sending to and receiving from a business logic(BL) that sends to and receives from a database(DB). The latest challenge is receiving from the BL and populating fields on the GUI. One of the problems I solved. So I have a custom widget that declares another custom widget.


class custom : public QWidget
{
private:
        Participants *claimant;
}

In the constructor of custom:

claimant = new Participants(ui->widget);

This uses the QWidget on the view as the parent so that's where it gets rendered. Then in a function to populate the fields I used this:

claimant->setName(recieved->getClaimant().getName());

The getName function is a function in a class that gets the name field for a person class I made. The setName is in the Paricipants widget that populates a lineEdit.


void Participants::setName(std::string str)
{
         ui->nameLineEditPar->setText(QString::fromStdString(str));

}


 Hope this helps anyone struggling with Qt. Any suggestion on other ways, or improvements would be gladly appreciated.




Friday, May 23, 2014

Space Engineers

Space Engineers from Keen Software House

    This sandbox in space game is "Minecraft" meets "Kerbal Space Program," using building blocks to create the space ship, and space stations of your dreams. This game is very nice even though it's still in beta. Great graphics are just the tip of the ice berg for this game. Real physics that crumple and destroy the blocks when hit. Meteor showers that rain destruction onto your ships and stations. Computer controlled ships that pass through the area for you to loot and salvage. Plus the developers aren't done yet, there will be more to come.

    Building and maintaining the space ships and stations is what this game is all about. With multiplayer support for up to 16 there's the possibility to form teams, and play a team death match, or play completely co-op. There's a creative game type that allows players to build what ever their heart desires with endless resources, and no health damage. Also a newly  added (May, 2014) survival game type that requires players to mine the asteroids for resources, refine them into usable materials, and produce the parts needed to build the ships. I find the mining of resources very relaxing almost zen like, but that me. There is also missile launchers, auto turrets, and other weapons to defend what you've build, or try and take what others have built.

    Some of the down sides of survival mode, which may be changed in the future, are you can't just start or respawn with out a ship. Currently if your character dies you respawn in a starter ship. With out this starter ship you don't have any way of refining the materials to make parts, or means of recharging your space suit. If you're trying to play a scenario where you want to gather the resources yourselves there's the temptation of using the starter ship to get them, or killing yourself to keep getting more and more starter ships. There are also no planets in this sandbox game.

    All in all I really enjoy this game. The graphics, and the survival mode really sold it for me. You might enjoy it as well, check it out with the link at the top.

Wednesday, May 21, 2014

The Start

I'm Tweety79rw, I have a youtube channel where I do let's plays of video games. This is just a test if I feel that I'm going to have time to do blogs. Heck I don't really know what blogs are about or how to write one, but here goes.