You just have too many destinations for too short a time. Paris and Barcelona have large numbers of attractions. If you like art, Madrid will take a lot of time as well. If you don't like art, you can skip Madrid and just go to Toledo. Nice is a pleasant, large city with some quite good art museums and markets. Otherwise, it's a base for seeing other places (charming though typically touristy) on the Riviera and up in the nearby hills. To go all the way to that area and spend only perhaps two days there doesn't really make any sense.
I'm puzzled about why you chose Porto but not Lisbon. They're both worth visiting, but I think there's more to see in Lisbon, plus there are the palaces in nearby Sintra. I'd skip the Algarve completely.
Whatever you do, you don't want to travel from Spain to Portugal and back to Spain. Transportation between those two countries is very limited and, except for flying, time-consuming. Don't do it twice.
Use the Deutsche Bahn website to check travel times by rail; you'll may need to use early-spring dates to get an approximation of the schedule for your travel period. Buses usually take quite a bit longer. Use Skyscanner to check for flights; they can be cheap if bought early.
For rail fares you'll need to visit the website of the company running the trains or perhaps trainline.eu, which sells tickets for most if not all of them. The super-low train and airfares you can snap up when tickets initially go on sale are non-refundable and non-changeable, so wait until you're sure of your schedule before buying.