Hello All!
I am building a dialog based app for symbian that teaches the user how to tie knots.
I am using a listwidget in the main dialog as a menu that passes the selected knot name to the knot view dialog that iterates through the images and displays them with a QLabel. I also display a text with a description of the selected knot. I have managed to get all that working with the code below. Because some knots like the bowline and sheet bend have a couple of different variants I used a switch function to be sure I get the correct images and description, this all works great though I wonder if this is the correct approach?
I would now like to iterate through the direction text's on a label below the displayed image.
I can make it work by building a separate file for each direction and then iterate through them the same way I am with the images and description documents but it seems cumbersome and I am thinking there must be a better way.
I can figure out how to use QString::indexOf(); to find key words in a document but is there a way to extract an entire sentence instead of just a word? That way I could just use one txt document for all the instructions...
All constructive criticism is welcomed!
If this code looks ridiculous go easy on me, I am learning C++ from a book...
Qt Code:
  1. //recieve knot name and index of selected knot
  2. void knotViewDlg::getIndex(QString knot, int index)
  3. {
  4. //setup d with the path to the resource images
  5. QStringList result; //holds the image filenames
  6. QStringList description; //holds the txt filenames
  7. QDir d(":/knotImages/images");
  8. //populate result with all image names
  9. result = d.entryList();
  10. knot.remove(" "); //remove spaces from string
  11. //search result for key word
  12. QString str1 = knot; //store selected knot name for txts
  13. QString str;
  14. strLst = result.filter(knot, Qt::CaseInsensitive); //fill strLst with filtered images
  15.  
  16. //figure out which bowline or sheet bend was selected then setup to select proper image files
  17. if(strLst.contains("Bowline_0.gif", Qt::CaseInsensitive))
  18. {
  19. switch(index)
  20. {
  21. case 0:
  22. {
  23. knot = "Bowline_";
  24. break;
  25. }
  26. case 1:
  27. {
  28. knot = "Doublebowline_";
  29. break;
  30. }
  31. case 2:
  32. {
  33. knot = "Bowlineonabight_";
  34. break;
  35. }
  36.  
  37. default:
  38. break;
  39. };
  40. strLst = result.filter(knot); //setup strLst with the correct image files
  41. strLst.sort();
  42. }
  43.  
  44. if(strLst.contains("Sheetbend_0.gif", Qt::CaseInsensitive) || (description.contains("DoubleSheetbend_0.gif", Qt::CaseInsensitive)))
  45. {
  46. switch(index)
  47. {
  48. case 3:
  49. {
  50. knot = "Sheetbend_";
  51. break;
  52. }
  53. case 4:
  54. {
  55. knot = "Doublesheetbend_";
  56. break;
  57. }
  58. default:
  59. break;
  60. };
  61. strLst = result.filter(knot);
  62. strLst.sort();
  63. }
  64. strLst.sort();
  65. //set first image before calling timer
  66. str = strLst.at(0);
  67. ui->label->setPixmap(QPixmap(QString::fromUtf8(
  68. ":/knotImages/images/").append(str)));
  69. //set first instruction here?
  70.  
  71. //setup description page
  72. d = (":/images/docs/texts");
  73. result = d.entryList();
  74. description = (result.filter(str1, Qt::CaseInsensitive));
  75. //figure out which bowline or sheet bend was selected then setup to select proper text file
  76. if(description.contains("Bowline.txt", Qt::CaseInsensitive))
  77. {
  78. switch(index)
  79. {
  80. case 0:
  81. {
  82. description.first() = "Bowline.txt";
  83. break;
  84. }
  85. case 1:
  86. {
  87. description.first() = "DoubleBowline.txt";
  88. break;
  89. }
  90. case 2:
  91. {
  92. description.first() = "Bowlineonabight.txt";
  93. break;
  94. }
  95. default:
  96. break;
  97. };
  98. }
  99. if(description.contains("Sheetbend.txt", Qt::CaseInsensitive) || (description.contains("DoubleSheetbend.txt", Qt::CaseInsensitive)))
  100. {
  101. switch(index)
  102. {
  103. case 3:
  104. {
  105. description.first() = "SheetBend.txt";
  106. break;
  107. }
  108. case 4:
  109. {
  110. description.first() = "DoubleSheetbend.txt";
  111. break;
  112. }
  113. default:
  114. break;
  115. };
  116. }
  117.  
  118. //open appropriate file for descriptions
  119. str = (QString::fromUtf8(":/images/docs/texts/").append(description.at(0)));
  120. QFile inputFile(str);
  121. inputFile.open(QIODevice::ReadOnly);
  122. QTextStream in(&inputFile);
  123. QString line = in.readAll();
  124. inputFile.close();
  125. ui->textEdit->append(line);
  126. ui->tabWidget->setCurrentIndex(1);
  127. ui->textEdit->setFocus();
  128.  
  129. //open appropriate instructions here?
  130. }
  131. void knotViewDlg::changeImage()
  132. {
  133. QString str = strLst.at(counter);
  134. ui->label->setPixmap(QPixmap(QString::fromUtf8(
  135. ":/knotImages/images/").append(str)));
  136. counter++;
  137. if(counter >= (strLst.count()))
  138. {
  139. counter = 0;
  140. }
To copy to clipboard, switch view to plain text mode 

All constructive comments and help much appreciated!

Cheers,
Jon