My first scala steps

2Disclaimer(this is not a tutorial or how to, i am a newb to scala and i recod my thoughts)

I was reading about scala for a time now and yesterday i started coding. I started with a simple application, making some simple function and try to get used with the syntax. So i started coding without looking in the book(Programing in scala) or on the web(to really learn it,i used to copy paste or read then type code in the scala interpretor but those methods don’t let you make mistakes that will help you remember )



package scalaprim

object Main {

  /**
   * @param args the command line arguments
   */
  def main(args: Array[String]): Unit = {
    println("Hello, world!")
    println(divide(10,2))
    println(divide(10,3))
    println(isPrime(11))
    println(isPrime(4))
    //findPrimes(100)
    val primes=findPrimes3(100)
    //primes.foreach(println)
  //consecutivePrimes(primes);
  consecutivePrimes2(primes);
  }
def divide(number:Int,divizor:Int):Boolean=(number%divizor==0);
def isPrime(number:Int):Boolean={

  val range = 2.until(Math.sqrt(number).intValue +1)
  for (i <- range)
    {
    if(divide(number, i))
     return false;
  }
  return  true;
}
def findPrimes(number:Int):Unit={
    for(i <-2.until(number))
      {
        if(isPrime(i))
          println(i)
      }
  }
  def findPrimes2(number:Int):IndexedSeq[Int]={
    val range=2 until number+1
    val primes=range.filter(isPrime)
    return primes
  }
  def findPrimes3(number:Int):IndexedSeq[Int]={
    (2 until number+1)filter isPrime   
  }

  def consecutivePrimes(listOfPrimes:IndexedSeq[Int]):Unit={
    var i:Int=0;
    while(i{
        if(prev+2==curent)
          println(prev,",",curent)
         curent
        }})
  }
}


My conclusion on this project is that using scala i can write less code, especially it could be possible to skip using lots of for, i worked on a c++ program and i think i have more then 100 fors, and nested fors that could be easily replaced in scala with calls to filter or exist or something else. And reducing for is not only about code clarity but often you can make bugs in for loops, like mistaking the indices in nested loops or getting outside of the array bounds.

I will investigate Scala in my free time and hope i could put it to use in a large project.

P.S. blogilo is failing uploading my posts, hope eventualy this post will be uploaded(finding a fix and trying another apps)

C++ debugging tip(for gcc and STL)

I want to wrote this tip down for me,to find it when i forget the details and for others that are searcing for it.

So if you are using STL containers like a vector,some strange errors errors can appear if you use an out of range index to the container. If you use .at() method then you are safe ,but you used [] in a lot of places and you can use the flag -D_GLIBCXX_DEBUG to use a safe STL container that will check and will find this errors.

The problem with this errors is theat the program will crash usualy in other place not in the place were the bug is. In my case the program crashed in the ~vector destructor. Read on google about using this flags have some problems(the important one is that you must rebuild all the parts that interoperate with STL containers).

To add this flag in a Qt project you need to add this line in the .pro file

QMAKE_CXXFLAGS +=-D_GLIBCXX_DEBUG1

Extracting webpage content using Qt4

Sometimes we want to check a web page or even to extract it’s content but we do not need to get all the information in the page, the images, the format ,we want only the essential information, In this case I will present I needed a application that will check a forum for new posts and display only the important stuff to me(no adds or other stuff). This particular forum has no mechanism to notify a member if no posts appear in one topic. This program works without using an account.

This can be done very easy in Qt using the class QWebView. An object of this class that in my code is declared

QWebView *view;

can display a web page. The important feature that i use is not displaying the page but executing JavaScript code on the page,so instead of using RegExp or other techniques to extract the information i use a JavaScript query

//get the main frame

QWebFrame *frame=view->page()->mainFrame();

if(frame==NULL)

return "NULL";

//get the elements i need

QWebElementCollection elements=frame->findAllElements("*.tr_list_f");

My application formats this data and stores it and displays it,here is the code

QString RL_HtmlParser::onPageLoad(bool ok)

{

if(!ok)

throw 11;//throw a exception , create a nice exception class later

//get the main frame

QWebFrame *frame=view->page()->mainFrame();

if(frame==NULL)

return "NULL";

//get the elements i need

QWebElementCollection elements=frame->findAllElements("*.tr_list_f");

int length=elements.count();

if(0==length)

return "NO MATCH FOUND";

QString sb("<html><head></head><body><table border=’3′>");

for(int i=0;i<length;i++)

{

if(0==i%2) continue;

sb+="<tr><td> "+elements.at(i).toPlainText()+"</td></tr>";

}

sb+="</table></body></html>";

//old text contains the old data that was read

//so if we have an update we store this data

if(sb!=oldText)

{

oldText=sb;

settings->setValue("oldtext",oldText);

}

//we display the current data

this->view->setHtml(sb);

//and return it as a string

return sb;

}

This is how a web page look like

and this is the application’

It is possible to use proxy and authentication to login and get the page, i downloaded the page in a temporary file and then i loaded it up in the webview but is possible to load it directly from the web

Installing windows sucks

I had to install a windows on my laptop to do some windows related work. I chose to install XP ,Win 7 is to large to consider it . I must confess that i hate windows interface, i have a disability and i need to use large fonts and zoom tools. The problem in windows is that the zoom tool sucks in comparison with the zoom effect in compiz or kwin(in Linux) and is impossible to make all the windows app to use same fonts e.g. Skype or Yahoo Messenger , i can’t really use large fonts, YM uses some html stuff so the settings in appearance have no effect on it, the only option is to use a small resolution like 800×600 that makes all controls large and windows not fitting the screen.

About the installation, i chose to install on the second partition,all was OK until restarting when i received a message like "Invalid partition table", and i was thinking "the stupid windows destroyed my partition table", i booted from a live CD, confirmed that the partitions are OK , recovered my GRUB from the first partition(GRUB is the Linux boot loader) and i added the windows to the Linux boot loader entry. After restart i chose to use windows from GRUB menu and i continued to install it.

After installation comes the hard part , install everything else. First i disabled the system restore then i installed my drivers from the CD,. After installing the drivers using the CD wizard and restarting i noticed that no driver was installed, i clean up the CD and CD-ROM unit ,reinstalled the driver and finally all was OK. The stupid driver wizard skipped installing the drivers because it had problems reading the CD but failed to tell me about the problems.

After the drivers i installed FireFox and Opera , Skype and Yahoo messenger, and hunted for a nice theme(still searching) i get bored of the classic XP theme. I Searched for a zoom tool but failed and currently i am writing this blog from my KDE/;Linux install, where the fonts are large and smooth.

It remains to do:

install antivirus,antispyware/malware stuff, maybe a firewall, a winrar like program, VLC, pdf reader, Open Office, a better image viewer , a better text editor, find a nice theme but first patch the dlls because you ave to pay money to M$ to change your themes.

I will have to search for new drivers manually, (the ATI driver from CD has some bugs).

For programming i must think if i reinstall some Visual studio 2008 or 2010 express or some trial, maybe i install it when i need it.

How an install works in Linux:

1 you will have no problem with the boot loader

2 you will have driver for all your parts(not all the time, some hardware may be not supported yet) but in my case all works in Linux, card reader,wireless, everything.

3 you do not need to browse the web to install apps ,you use your package manager

4 no antivirus and other stuff to run in background

5 no annoying stuff installed and starting and showing up in tray, like wireless indicators when no wireless is present and i do not use my wireless card, or when you install Yahoo messenger toolbars and other stuff appear in your browser (are installed behind your back)

The desktop does not get filed with icons, in Linux you have a nice structured menu ,using categories .

and more, maybe i will rite later about using windows pains.

Anunt/Anouncement

Fac programe la cerere / comanda,realizez lucrari de licenta/disertatii atestate, in domeniul informaticii >Limbaje de programare: c,c++,c#,java,javascript lucrez si cu qt

I do software /progrms /applications on demand .Programming languages :c ,c++,c#,java javascript. I do homeworks /papers for exams etc.

Contact:

simion314 &@& y a h o o & .& c o m (REMOVE the dolar sing & , i placed it there to aviod my email beeing picked up by spamers) SCOATE semnul dolar & , e pus acolo ca spameri sa nu imi fure emailul

Or leave a comment

SAU lasa un comentariu

Link http://simion314.isgreat.org/

Creating a Image,Picture widget in Qt4 that keeps the aspect ration has zoom and rotate features

To display image in Qt4 you can use a QLabel and the setPixmap() method. But if you need something more complex that will keep the aspect ratio and can zoom and rotate you will have to create a custom widget. This is how i did it for a Image viewer application that i make it in Qt4 using c++.

#ifndef IMAGEWIDGET_H

#define IMAGEWIDGET_H

#include <QWidget>

class QSize;

class QImage;

class ImageWidget : public QWidget

{

Q_OBJECT

public:

explicit ImageWidget(QWidget *parent = 0);

void setImage(const QImage &image);

QImage *getImage();

float getZoomFactor();

//this is absolute depends only on the size of the image

void setZoomFactor(float zf);

//this is relative with the curent size/zoom

void zoom(float zoomWithThisFactor);

//if set to true the image will be zoomed to fit the widget

void setZoomToFit(bool ztf);

signals:

public slots:

protected:

//custom drawing rutine

void paintEvent(QPaintEvent *);

//custom sizeHint method

QSize sizeHint() const;

private:

QImage *image;

float zoomFactor;

float aspectRatio;

bool zoomToFit;

void computeZoomFactorToFitImage();

void adjustSize();

};

#endif // IMAGEWIDGET_H

I subclased QWidget and reimpemented 2 methods paintEvent() and sizeHint():

void ImageWidget::paintEvent(QPaintEvent *)

{

int x,y,w,h;

//compute the image displaty size by multipling with the zoom factor

w=image->width()*zoomFactor;

h=image->height()*zoomFactor;

//compute the x and y offset

x=(rect().width()-w)/2;

y=(rect().height()-h)/2;

//if from some strange bug we get a negative value,

//probly this must be changed to an assert or thow and error and fix the eventual problem

x=qMax(x,0);

y=qMax(y,0);

QRectF target(x,y,w,h);

QPainter p(this);

p.drawImage(target,*image);

}

QSize ImageWidget::sizeHint() const

{

if(image!=NULL)

{

QSize hint= image->size();

hint=zoomFactor*hint;

return hint;

}else

return this->minimumSize();

}

The zoomToFit method will compute the zoom factor to fit the widget and will zoom the image

void ImageWidget::setZoomToFit(bool ztf)

{

this->zoomToFit=ztf;

if(zoomToFit)

{

computeZoomFactorToFitImage();

}

else

{

zoomFactor=1.0;

}

adjustSize();

this->update();

}

void ImageWidget::computeZoomFactorToFitImage()

{

//compute the aspect ration of the widget

//cast t float to get a float result, if forget (float) you will get

//a int value

float ratio=rect().width()/(float)rect().height();

if(ratio>aspectRatio)

{//the widget width is larger relative of the image wirth

zoomFactor=rect().height()/(float)image->height();

}

else

{

zoomFactor=rect().width()/(float)image->width();

}

}

void ImageWidget::adjustSize()

{

resize(this->sizeHint());

}

You can find all the code on source forge in a Image viewer application, the application is licnsed under GPL v3 , if someone needs the code under other license email me and i can give the rights

UPDATE:

download

svn co https://kisviwer.svn.sourceforge.net/svnroot/kisviwer kisviwer

you must use a svn client or the svn command line application, if in linux install svn and paste that line in the terminal,and a drectory kisviwer will appear with the code,windws guys google it

a simple image viewer ,Qt4

Updates to the educational software, added coloring game and memory game

I am working on some education software for young kids, less then 7 years old. I joined the jde educational project and i hope that eventually this games will be part of the kde-edu distributions.

I updated the Learn to Count application, now it can be chossed at start the maximum number of items that will appear. So if the kid has problems counting a large number of items you can limit the program to display less items.

Chose the maximum number to use dialog
Chose the maximum number to use dialog

The coloring application was a little hard to code, I had to optimize the bucket fill algorithm.Now it has a good performance on my computer, i must test on older computers.

The screen fot selecting the image to work on, to color it
Select image screen
Coloring a flower image, this is the main view
Coloring a flower image, this is the main view

2

The memory game was easy to code, after adding some animations I think that it could be refactored a little to be easy to read the code.

I found a super web site that contains free artwork that can be used with no restrictions.  http://openclipart.org/

Educational software

I have a 3 and 1/2 years old boy, I learned him to se the mouse and the arrow keys to play some basic games. To help him learn to count I created a little game named Learn to count,written in c++ and Qt. My son knows the symbols “1”,”2″,…”10″ and it knows to count correctly but it is harder for him to count objects. So if you put him to count some objects he will fail sometimes. Using this game he can practice this and practice the use of the mouse.

I used sourceforge.net to host the project,it was my first time i used it so i had some problems getting used to it.

The application is posted to qt-apps.org

The application is licensed under GPL v3 , the art was made by my wife Cristina, and I thank her for the help.

How to add skins/themes/styles to a Qt application

Today I continued working on my ffmpeg wrapper application, after I get bored I tried to make something more interesting and I wanted to make the user able to change the theme of the application.

First I read the example from here http://doc.trolltech.com/4.3/widgets-styles.html  ,then i downloaded the widgetgallery files and added to my project and remove the entries of the norwegian style(created I that example).I am running on kubuntu and i have access to some basics themes but I do not know if the windows user can access them. I will try this scenario after building the application on my windows virtual machine.