Friday, November 7, 2008

Java Basics Using Qt Jambi Tutorial


/********************************************************************************
** Java Using Qt Jambi Tutorial Example PhotoSlideShow
**
** Created: Nov. 7 2008
** by: Fernando L. Cuevas Jr
**
********************************************************************************/
package qt.book.example.photoslideshow;
import com.trolltech.qt.gui.*;
import com.trolltech.qt.phonon.AudioOutput;
import com.trolltech.qt.phonon.MediaObject;
import com.trolltech.qt.phonon.MediaSource;
import com.trolltech.qt.phonon.Phonon;

import java.lang.String;
import java.util.List;
public class PhotoSlideShowMainClass {
private static List fileNames;
private static StringBuilder sbFileNames = new StringBuilder();
List fileName;
private static String labelMusicText;
private static MediaObject mediaObject = new MediaObject();
private static int numberOfPics = 0;
private static String[] cntrFileNames;
private static int cntrPixmap = 0;
private static boolean timeInitial=true;
private static Long picsTime;
private static Long picsTime2;
private static int cntrFiles = 0;

public void addPhotos(){

String filterPhotos = new String("Images (*.jpg *.png)");
QFileDialog dialog = new QFileDialog();//create instance of file dialog
dialog.setDirectory("/home"); //modify this depending on OS
dialog.setFilter(filterPhotos);//this filters what it gets selected in file dialog *.jpg *.png
dialog.setFileMode(QFileDialog.FileMode.ExistingFiles);//selects existing files
dialog.show();//shows the file dialog

if (dialog.exec() != 0){ //if ok button is clicked
fileNames = dialog.selectedFiles(); //gets all selected filenames
}

int row = -1;

for (String files : fileNames){ //parses each file name in fileNames selected
QListWidgetItem item = new QListWidgetItem();
item.setIcon(new QIcon(files)); //inserts the thumbnails in listwidget
item.setToolTip(files);
Ui_PhotoSlideShowMainWindow.listWidget.insertItem(row, item); //inserts the items in existing widget
sbFileNames.append(files + ";"); //you may use a fixed length array
numberOfPics++;
}
//System.out.println(sbFileNames.toString());
}
public void addMusic(){

String filterMusic = new String("Music File (*.mp3 *.wma)");
QFileDialog dialog = new QFileDialog();
dialog.setFilter(filterMusic);
dialog.setFileMode(QFileDialog.FileMode.ExistingFile);
dialog.show();

if (dialog.exec() != 0){
fileName = dialog.selectedFiles();
}

for (String labelText : fileName){
labelMusicText = labelText;
}
int i;
int cntr = labelMusicText.length();

for (i=labelMusicText.length(); i > 0; i--){ //this for loop extracts the file name only from path
if (labelMusicText.charAt(i-1) != '/'){
cntr--;
}
else{
break;
}
}
Ui_PhotoSlideShowMainWindow.label_Music.setText(labelMusicText.substring(cntr, labelMusicText.length()));

for (i=0;i < sbFileNames.length(); i++){
if (sbFileNames.charAt(i)==';'){
cntrFiles++;
}
}

cntrFileNames = new String[cntrFiles];
int cntrJ=0;
int cntrK=0;
int cntrI=0;
for (i=0;i < sbFileNames.length(); i++){
if (sbFileNames.charAt(i)==';'){
cntrFileNames[cntrI] = sbFileNames.substring(cntrK,i);
//System.out.println(cntrFileNames[cntrI]);
cntrK = cntrJ + 1;
cntrJ++;
cntrI++;
}
else{
cntrJ++;
}
}

PhotoSlideShowMainClass playmusic = new PhotoSlideShowMainClass();
Ui_PhotoSlideShowMainWindow.pushButton_Play.clicked.connect(playmusic, "playMusic()"); //when play button is clicked, call playMusic() function

}

public void playMusic(){
//this creates the object to play music file(start)
mediaObject.setCurrentSource(new MediaSource(labelMusicText));
AudioOutput audioOutput = new AudioOutput(Phonon.Category.MusicCategory);
audioOutput.setVolume(70);
Phonon.createPath(mediaObject, audioOutput);
//this creates the object to play music file(end)
mediaObject.play(); //plays the source file
mediaObject.setTickInterval(1000); //change the tick interval to 1 second
mediaObject.tick.connect(this, "tick(long)");
}
@SuppressWarnings("unused")
private void tick(long time){ //this is called every 1 second
long totalTime = mediaObject.totalTime();
Ui_PhotoSlideShowMainWindow.horizontalSlider.setMaximum((int)totalTime); //sets the maximum time of slider
int sliderTime = (int)mediaObject.currentTime();
Ui_PhotoSlideShowMainWindow.horizontalSlider.setValue(sliderTime);
if (timeInitial){ //gets the time of every pic during playback
picsTime = totalTime / numberOfPics;
picsTime2 = picsTime;
timeInitial=false;
}
Ui_PhotoSlideShowMainWindow.label_Photo.setPixmap(new QPixmap(cntrFileNames[cntrPixmap]));

if (picsTime < mediaObject.currentTime()){
cntrPixmap++;
Ui_PhotoSlideShowMainWindow.label_Photo.setPixmap(new QPixmap(cntrFileNames[cntrPixmap])); //loads the picture files to label_photo
picsTime = picsTime + picsTime2;
}
}
public void deletePhotos(){

}
public void deleteMusic(){

}
public void playSlideShow(){

}
public void pauseSlideShow(){

}
public void stopSlideShow(){

}
}

1 comment:

Fodil said...

thank you, it was helpful for me